0

Data:

<5b005c00 5d005e00 5f006000 61006200 63006400>

Output: 6029403

The above data length is 20bytes, Its supposed to return value in the range of 0-100,

Code I used:

ppData = [ppData subdataWithRange:NSMakeRange(0, 20)];

int value = *(int*)([ppData bytes]);
NSLog(@"int rec %d",value);
Sai Dammu
  • 11
  • 2
  • 2
    An `int` is likely only 4 bytes. The first 4 bytes you have `5b005c00` would be the decimal value `6029403` with the proper endianness. So please explain why you have 20 bytes and why your say the output should be 6029403 yet you also say it's supposed to be in the range 0-100. What does that mean? – rmaddy Oct 28 '19 at 18:10

1 Answers1

0

Your data appears to be the 10 16-bit integers: 91, 92, ..., 100; laid out in little-endian format. MacOS & iOS are little-endian[*] so to get the first value as an int change your code to:

int value = *(int16_t *)ppData.bytes;

The (Objective-)C language itself takes care of the conversion of the int16_t value from the RHS into the int (32-bit on MacOS/iOS) value required to store into value.

HTH

[*] When the platform and data endianness differ you need to call the appropriate byte-swapping functions that are usually provided.

CRD
  • 52,522
  • 5
  • 70
  • 86