Search code examples
objective-cxcodeintvalueconverteruint8t

How convert int8_t array into integer value in objective c?


I have the following array :

static const int8_t ARRAY[8] = {0x18, 0xB8, 0xCE, 0x0, 0x0, 0x0, 0x0, 0x0};

and I need to get an integer value. For this ARRAY final int value is 13547544, because the bytes in the array, followed by in the opposite order little-endian.

Example 0xCEB818 = 13547544

How can I do this? Can have ready standard solutions?

Thanks in advance!


Solution

  • Since the host platform is little-endian, you can just point a 64-bit integer pointer at the array and the machine will read the array correctly.

    static const int8_t ARRAY[8] = {0x18, 0xB8, 0xCE, 0x0, 0x0, 0x0, 0x0, 0x0};
    uint64_t *value = (uint64_t *)&ARRAY;
    
    NSLog(@"%llu", *value); // outputs 13547544