gpt4 book ai didi

c - 从 unsigned int[2] 加倍?

转载 作者:行者123 更新时间:2023-12-04 11:45:15 26 4
gpt4 key购买 nike

我有一个 64 位数字,写成两个 32 位未签名的整数:unsigned int[2]unsigned int[0] 是 MSB,unsigned int[1] 是 LSB。我如何将它转换为 double

double d_from_u2(unsigned int*);

最佳答案

memcpy 它以正确的顺序从您的源数组到 double 对象。例如。如果你想交换 unsigned 部分

unsigned src[2] = { ... };
double dst;
assert(sizeof dst == sizeof src);

memcpy(&dst, &src[1], sizeof(unsigned));
memcpy((unsigned char *) &dst + sizeof(unsigned), &src[0], sizeof(unsigned));

当然,您始终可以将源对象和目标对象重新解释为 unsigned char 数组,并以您希望的任何顺序逐字节复制它们

unsigned src[2] = { ... };
double dst;

unsigned char *src_bytes = (unsigned char *) src;
unsigned char *dst_bytes = (unsigned char *) &dst;

assert(sizeof dst == 8 && sizeof src == 8);

dst_bytes[0] = src_bytes[7];
dst_bytes[1] = src_bytes[6];
...
dst_bytes[7] = src_bytes[0];

(第二个示例并不等同于第一个示例。)

关于c - 从 unsigned int[2] 加倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42116327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com