编写一个函数,该函数将返回一个值,其中i
-th 字节x
已被 替换b
:
unsigned replace_f (unsigned x, int i, unsigned char b){
}
前任:replace_f(0x12345678, 2, 0xBC) --> 0x12BC5678
没试过,但这可以工作:
unsigned replace_f (unsigned x, int i, unsigned char b){
char *c;
c = (char *)&x;
c[i] = b;
return x;
}
unsigned replace_f (unsigned x, int i, unsigned char b){
unsigned char *place = (unsigned char*)&x;
place[sizeof(int)-i] = b;
return x;
}
假设小端