i
の- 番目のバイトが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;
}
リトルエンディアンを想定