用无符号整数中的另一个字节替换第n个字节

用无符号整数中的另一个字节替换第n个字节

问题描述:

我想用b替换a的字节。用无符号整数中的另一个字节替换第n个字节

unsigned int a = 0x12dc4430; 
unsigned char b = 0xcb; 

如何用b替换第一个和第三个字节?

像这样: 为1显著字节

12dc33cb 

和第三显著字节

12cb4430 

不知道你所说的“第1和第3个字节”的意思,但假设你的意思最重要的字节和第三最重要的字节:

unsigned int a = 0x12dc4430; 
unsigned char b = 0xcb; 
a = (a & 0x00ff00ff) | (b << 8) | (b << 24); 
printf("%x\n", a); 

Pr ints cbdccb30