将字节数组转换为double - c

pao*_*lo_ 8 c bytearray

我试图从16个元素的字节数组中获取数值(double)值,如下所示:

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;
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.似乎包含左移结果的临时变量只能存储32位,因为在8位的4位移位操作之后它会溢出.

我知道我可以使用类似的东西

a = input[i] * pow(2,8*i);
Run Code Online (Sandbox Code Playgroud)

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

Adr*_*iuk 4

编辑:如果没有类似的东西,这将无法工作(请参阅评论)__int128

a = input[i] << 8*i;
Run Code Online (Sandbox Code Playgroud)

该表达式input[i]被提升为int( 6.3.1.1 ) ,它在您的计算机上是 32 位的。为了解决这个问题,左侧操作数必须是 64 位,就像

a = (1L * input[i]) << 8*i;
Run Code Online (Sandbox Code Playgroud)

或者

a = (long long unsigned) input[i] << 8*i;
Run Code Online (Sandbox Code Playgroud)

并记住字节序