3

I have an NSData packet with data in it. I need to convert the byte at range 8, 1 to an int. To get the data at that location I do the following.

NSData *byte = [packet subdataWithRange:NSMakeRange(8, 1)];

If I NSLog byte

<01>

How do I think convert this to an int? This is probably the most basic of questions but I am just not getting it right.

Any help would be appreciated.

Update

With that data the int should be equal to 1. I am not sure if this has anything todo with Endian.

sbarow
  • 2,759
  • 1
  • 22
  • 37

1 Answers1

5

use -[NSData bytes] to get raw buffer and read from it

int i = *((char *)[byte bytes])

or use -[NSData getBytes:length:]

char buff;
[bytes getBytes:&buff length:1];
int i = buff;

make sure you are reading from char * not int *, otherwise you are accessing invalid memory location, which may or may not crash or provide correct result.

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143