Converting unsigned chars to signed integer

前端 未结 4 2222
轻奢々
轻奢々 2021-02-09 20:11

I have an unsigned char array with 2 elements that represents a signed integer. How can I convert these 2 bytes into a signed integer?

Edit: The unsigne

4条回答
  •  既然无缘
    2021-02-09 20:53

    It depend of endianness. Something for big endian :

    unsigned char x[2];
    short y = (x[0] << 8) | x[1]
    

    Something for little endian :

    unsigned char x[2];
    short y = (x[1] << 8) | x[0]
    

提交回复
热议问题