如何在C++中将无符号字符缓冲区转换为双精度?
How do I convert an unsigned char buffer to a double in C++?
我正在尝试将unsigned char
缓冲区数组转换为双精度数组。 为什么这不会像在数组中那样将字节复制到双精度中?
#include <iostream>
#include <string>
int main() {
unsigned char buffer[8] = {63, 240, 0, 0, 0, 0, 0, 0};
double x = *(double*)buffer;
std::cout << x << std::endl;
return 0;
}
我也尝试这样做:
#include <iostream>
#include <string>
int main() {
unsigned char buffer[8] = {63, 240, 0, 0, 0, 0, 0, 0};
double x ;
memcpy(&x, buffer, sizeof(double)); //NOW USING MEMCPY
std::cout << x << std::endl;
return 0;
}
我在这里看了这篇文章,但它只得到了相同的结果。 无符号字符 {63, 240, 0, 0, 0, 0, 0, 0} 是双精度数1
的表示形式。
它输出:3.03865e-319
。
你的缓冲区用错了。 它应该是:
{0, 0, 0, 0, 0, 0, 240, 63}
(在使用 IEEE 浮点的小端计算机上(。
现场演示
相关文章:
- 将字符缓冲区强制转换为函数指针
- 如何在C++中将无符号字符缓冲区转换为双精度?
- 在 node.js 中将缓冲区从 C++ 转换为 UTF-8 字符串
- 如何将RGB像素缓冲区转换为IRandomAccessStream^
- 将缓冲区转换为在现有程序中工作的iStream
- 二进制字符数组转换为字符串流并从缓冲区弹出
- 将字符缓冲区转换为字符串
- 当需要将char缓冲区转换为字符串时
- 将协议缓冲区中的二进制双精度数组转换为 JavaScript 数字
- FlatBuffers C++从缓冲区转换为对象,仅适用于root_type(而Java具有所有)
- 操作方法:将 boost::endian 缓冲区类型转换回本机格式
- 将int转换为十六进制并将其存储到char缓冲区中
- 在使用OpenGl中使用转换反馈的GL_SEPARATE_ATTRIB时,如何读取多个缓冲区
- 将DER编码的X509证书缓冲区转换为Windows cert_context结构
- 无法将缓冲区 [200] 转换为 str[]
- 在 Python 中使用 OpenCV 将打包的 BGRA 图像缓冲区转换为 RGB 时遇到问题
- 如何将BGRA缓冲区转换为RGBA缓冲区格式
- 如何将字符 * 缓冲区转换为无符号的短 int * bufferSh
- 将windows BITMAP转换为PIX(无符号字符缓冲区)
- winsock,面向消息的网络,以及从recv中类型转换缓冲区