我在尝试将对齐数组 uint8[8]
转换为 double
时遇到了一些挑战。
使用位操作将 uint8[4]
转换为 long
特别容易,但我知道 double
会变得困惑符号位?
在 Java 中我简单地使用 ByteBuffer.wrap(bytes).getDouble()
但我认为它在 C 中并不那么容易。
我试图实现这段代码,但最后一条命令给出了错误 Expression is not assignable
and Shift count >= width of type
long tempHigh = 0;
long tempLow = 0;
double sum = 0;
tempHigh |= buffer[0] & 0xFF;
tempHigh <<= 8;
tempHigh |= buffer[1] & 0xFF;
tempHigh <<= 8;
tempHigh |= buffer[2] & 0xFF;
tempHigh <<= 8;
tempHigh |= buffer[3] & 0xFF;
tempLow |= buffer[4] & 0xFF;
tempLow <<= 8;
tempLow |= buffer[5] & 0xFF;
tempLow <<= 8;
tempLow |= buffer[6] & 0xFF;
tempLow <<= 8;
tempLow |= buffer[7] & 0xFF;
sum |= ((tempHigh & 0xFFFF) <<= 32) + (tempLow & 0xFFFF);
如何正确完成此过程或只是解决我犯的错误?
提前致谢。
最佳答案
double
是浮点型;它不支持位运算,例如 |
。
你可以这样做:
double sum;
memcpy(&sum, buffer, sizeof(sum));
但要注意字节顺序问题。
关于ios - 将对齐数组 uint8[8] 转换为 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10415074/