我有一个函数返回 1 Byte
uint8_t fun();
该函数应该运行 9 次,所以我得到 9 字节 我想将最后一个 8
作为 4 short values
这里我所做的,但是我'我不确定我得到的值是否正确:
char array[9];
.............
for ( i = 0; i< 9 ; i++){
array[i] = fun();
}
printf( " 1. Byte %x a = %d , b=%d c =%d \n" ,
array[0],
*(short*)&(array[1]),
*(short*)&(array[3]),
*(short*)&(array[5]),
*(short*)&(array[7]));
是吗?
最佳答案
最好明确一点,自己将 8 位值连接到 16 位值中:
uint8_t bytes[9];
uint16_t words[4];
words[0] = bytes[1] | (bytes[2] << 8);
words[1] = bytes[3] | (bytes[4] << 8);
words[2] = bytes[5] | (bytes[6] << 8);
words[3] = bytes[7] | (bytes[8] << 8);
顺便说一句,上面假设小端。
关于简而言之结合 MSB 和 LSB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29776235/