转换字节数组双 - ç [英] converting byte array to double - c

查看:169
本文介绍了转换字节数组双 - ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从16个元素的字节数组的数字(双)值,如下所示:

I'm trying to get the numerical (double) value from a byte array of 16 elements, as follows:

unsigned char input[16];
double output;
...
double a  = input[0];
distance = a;
for (i=1;i<16;i++){
    a = input[i] << 8*i;
    output += a;
}

,但它不工作。
似乎包含左移位的结果的临时变量只能存储32位,因为之后的8位的4移位运算溢出

but it does not work. It seems that the temporary variable that contains the result of the left-shift can store only 32 bits, because after 4 shift operations of 8 bits it overflows.

我知道我可以使用类似

a = input[i] * pow(2,8*i);

但是,出于好奇,我想知道是否有使用移位运算符对这个问题的任何解决方案...

but, for curiosity, I was wondering if there's any solution to this problem using the shift operator...

推荐答案

编辑:这不是没有类似 __ int128工作(见注释)

this won't work (see comment) without something like __int128.

a = input[i] << 8*i;

这位前pression 输入[I] 提升为 INT 6.3.1.1 ),这是你的机器上32位。为了克服这个问题,左侧操作数必须是64位,如在

The expression input[i] is promoted to int (6.3.1.1) , which is 32bit on your machine. To overcome this issue, the lefthand operand has to be 64bit, like in

a = (1L * input[i]) << 8*i;

a = (long long unsigned) input[i] << 8*i;

和记住字节顺序

这篇关于转换字节数组双 - ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆