如何在 C++ 中读取 short int(2 字节)LSB 第一个值?
How to read in C++ a short int (2 byte) LSB first value?
如何读取两个单独的字节,每个字节都使用 LSB 在前?
2 个单独的 LSB 第一个字节 -> 一个 short int MSB 在前
例如01001100
11001100
-> 00110010 00110011
short int lsbToMsb(char byte1, char byte2) {
...
return msb;
}
试试这个:
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);
}
输出:
12851 // 00110010 00110011
如何读取两个单独的字节,每个字节都使用 LSB 在前?
2 个单独的 LSB 第一个字节 -> 一个 short int MSB 在前
例如01001100 11001100 -> 00110010 00110011
short int lsbToMsb(char byte1, char byte2) {
...
return msb;
}
试试这个:
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);
}
输出:
12851 // 00110010 00110011