Convert binary two's complement data into integer in objective-c

后端 未结 1 1579
再見小時候
再見小時候 2020-12-11 13:51

I have some binary data (twos complement) coming from an accelerometer and I need to convert it to an integer. Is there a standard library function which does this, or do I

相关标签:
1条回答
  • 2020-12-11 13:57

    Something like:

    const int8_t* bytes = (const int8_t*) [nsDataObject bytes];
    
    int32_t x = (bytes[0] << 8) + (0x0FF & bytes[1]);
    x = x << 16;
    x = x >> 16;
    
    int32_t y = (bytes[2] << 8) + (0x0FF & bytes[3]);
    y = y << 16;
    y = y >> 16;
    
    int32_t z = (bytes[4] << 8) + (0x0FF & bytes[5]);
    z = z << 16;
    z = z >> 16;
    

    This assumes that the values really are "big-endian" as suggested in the question.

    0 讨论(0)
提交回复
热议问题