duck1970
duck1970

Reputation: 137

Decode bytes in NSData into integers

I have a 16 Bit Two's Complement characteristic from a BLE device. Its contains 9 integers at byte numbers: 0-1, 2-3, 4-5 .... to 16-17.

Data type is NSData and looks like this:

<faff0100 1b00ab00 daff9141 5603c0fd 06f2>

How do I extract the integers?

Upvotes: 0

Views: 228

Answers (1)

Codo
Codo

Reputation: 78795

Your data is basically a 9 element array of integers.

If you work with Objective C, you can access via the bytes method. I want to check is has the correct length first:

int16_t numberArray[9];
NSData* data = ...; // your NSData instance
memcpy(numberArray, [data bytes], 18);

NSLog(@"First number: %d", numberArray[0]);  // bytes 0-1
NSLog(@"Second number: %d", numberArray[1]); // bytes 2-3
NSLog(@"Third number: %d", numberArray[2]); // bytes 4-5
...
NSLog(@"Ninth number: %d", numberArray[8]); // bytes 16-17

Upvotes: 1

Related Questions