[英]How do I convert an unsigned char buffer to a double in C++?
I am trying to convert an unsigned char
buffer array into a double.我正在尝试将unsigned char
缓冲区数组转换为双精度。 Why does this not copy the bytes into the double as they are in the array?为什么这不会将字节复制到数组中的双精度数中?
#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;
}
I also tried doing this:我也试过这样做:
#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;
}
I looked at this post here , but it only got the same results.我在这里查看了这篇文章,但它只得到了相同的结果。 The unsigned chars {63, 240, 0, 0, 0, 0, 0, 0} is the representation of the double number 1
.无符号字符 {63, 240, 0, 0, 0, 0, 0, 0} 是双精度数1
的表示。
It outputs: 3.03865e-319
.它输出: 3.03865e-319
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.