Converting bytes array to integer

前端 未结 2 1848
既然无缘
既然无缘 2020-12-21 16:38

I have a 4-byte array (data) of type uint8_t, which represents a speed data integer. I\'m trying to cast this array to uint32_t integer (speed), m

2条回答
  •  别那么骄傲
    2020-12-21 17:16

    Your code doesn't work because during data=(uint8_t*)speed; you don't get a "lvalue" for data, you just get an array type which can't be used in assignment or any form of arithmetic. Similarly, speed=( uint32_t)*data; is a bug because that only gives you the first item in the array.

    The only correct way you should do this:

    volatile uint8_t data[4] = {0x00 , 0x00, 0x00, 0x00};
    volatile uint32_t speed;
    
    speed = (uint32_t)data[0] << 24 |
            (uint32_t)data[1] << 16 |
            (uint32_t)data[2] <<  8 |
            (uint32_t)data[3] <<  0;
    
    speed=speed*10;
    
    data[0] = (uint8_t) ((speed >> 24) & 0xFFu);
    data[1] = (uint8_t) ((speed >> 16) & 0xFFu);
    data[2] = (uint8_t) ((speed >>  8) & 0xFFu);
    data[3] = (uint8_t) ((speed >>  0) & 0xFFu);
    

    This is 100% portable and well-defined code. No implicit promotions take place. This code does not rely on endianess or other implementation-defined behavior. Why write code that does, when you can write code that doesn't?

提交回复
热议问题