[英]How to read in C++ a short int (2 byte) LSB first value?
How can I read two separate bytes each with LSB first?我怎样才能读取两个单独的字节,每个字节都先读取 LSB?
2 speparate LSB first bytes -> one short int MSB first 2 个单独的 LSB 第一个字节 -> 一个 short int MSB 在前
eg 01001100 11001100 -> 00110010 00110011例如 01001100 11001100 -> 00110010 00110011
short int lsbToMsb(char byte1, char byte2) {
...
return msb;
}
Try this:尝试这个:
char reverseBits(char byte)
{
char reverse_byte = 0;
for (int i = 0; i < 8; i++) {
if ((byte & (1 << i)))
reverse_byte |= 1 << (7 - i);
}
return reverse_byte;
}
short int lsbToMsb(char byte1, char byte2) {
byte1 = reverseBits(byte1);
byte2 = reverseBits(byte2);
short int msb = (byte1 << 8) | byte2;
return msb;
}
int main(){
char byte1 = 76; // 01001100
char byte2 = -52; // 11001100
short int msb = lsbToMsb(byte1, byte2);
printf("%d", msb);
}
Output: Output:
12851 // 00110010 00110011
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.