如何在 C++ 中将 unsigned char 缓冲区转换为 double?
How do I convert an unsigned char buffer to a double in C++?
我正在尝试将 unsigned char
缓冲区数组转换为双精度数组。
为什么这不会像在数组中那样将字节复制到 double 中?
#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;
}
我看了这个 post here,但它只得到了相同的结果。 unsigned chars {63, 240, 0, 0, 0, 0, 0, 0} 是双数 1
.
的表示
输出:3.03865e-319
.
你绕错了缓冲区。应该是:
{0, 0, 0, 0, 0, 0, 240, 63}
(在使用 IEEE 浮点的小端机器上)。
我正在尝试将 unsigned char
缓冲区数组转换为双精度数组。
为什么这不会像在数组中那样将字节复制到 double 中?
#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;
}
我看了这个 post here,但它只得到了相同的结果。 unsigned chars {63, 240, 0, 0, 0, 0, 0, 0} 是双数 1
.
输出:3.03865e-319
.
你绕错了缓冲区。应该是:
{0, 0, 0, 0, 0, 0, 240, 63}
(在使用 IEEE 浮点的小端机器上)。