ios - 将对齐数组 uint8[8] 转换为 double

标签 ios c double bit-manipulation core-foundation

我在尝试将对齐数组 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/

相关文章:

c# - 如何处理大多数小数不能用二进制准确表示的事实?

c++ - 如何提取 double 的尾数

ios - 在 Xcode 7 中使用 Bundle Identifier 而不是 Product Bundle Identifier

ios - 另一个表格 View 重新加载问题

ios - avplayerviewcontroller 控制事件

我可以告诉 MSVC 编译器不要使用某个寄存器吗?

java - 如何将 double[] 转换为 double 作为我的返回值? (不兼容类型错误)

ios - UIScrollView 中的 UIView 尊重一些约束,但不尊重其他约束

c - 减去 2 个带符号结果的指针,适合输入到红黑树 "comparator"

c - 在 C 不同变量中分割字符串