1

I've a NSData object. First I want to convert NSData object to bytes, then read the first four bytes in this NSData object and then convert the first four bytes to their equivalent integer values.

Any ideas on how to go about this?
How about converting all the four bytes to a single positive integer value?

ricardopereira
  • 11,118
  • 5
  • 63
  • 81
Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50
  • It's been already answered [here][1]. Whatever you do, mind endianess. [1]: http://stackoverflow.com/questions/3098808/int-from-nsdata – matm Sep 07 '11 at 07:51
  • Since he wants the int values of single bytes, endianness doesn't play a role. – Rudy Velthuis Sep 07 '11 at 08:07
  • FWIW, this is NOT the same question, AFAICT. Pranav doesn't want to interpret the first 4 bytes as one int, he wants the int values (plural) of the first four bytes. The principle is of course the same. – Rudy Velthuis Sep 07 '11 at 08:10
  • Your question is unclear. Do you want the bytes converted to characters, and the characters converted to a single int, the individual bytes converted to ints (which is barely a "conversion", since they already are), or the bytes converted to a single 4-byte int? – Hot Licks Sep 07 '11 at 11:37

3 Answers3

5

Getting an int out of raw data is ambiguous: where does the data come from? What size do you want your int? Do you want them signed or unsigned? What byte ordering do you expect?

So here is one scenario: the data you get is from a stream encoded by an external process that feeds 32-bit signed ints in big-endian order. Here's how you could do it:

NSData *dataFromStream = functionThatReturnsNSData();
SInt32 *signedInt32pointer = [dataFromStream bytes];
SInt32 unSwappedInt32 = *signedInt32pointer;
SInt32 reorderedInt32 = CFSwapInt32BigToHost(unSwappedInt32);

RTFM the Byte ordering and byte swapping sections of the Memory Management Programming Guide for Core Foundation.

Jean-Denis Muys
  • 6,772
  • 7
  • 45
  • 71
4

That's quite simple. Use bytes to get at the bytes and then cast to unsigned char*

unsigned char *n = [yourNSData bytes];
int value1 = n[0];
int value2 = n[1];
int value3 = n[2];
int value4 = n[3];

Update

To turn this into a single int assumes bytes contains a valid int:

int result = *(int *)n;
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • thanks for responding so quickly.another ques though, How about converting all the four bytes to a single int? – Pranav Jaiswal Sep 07 '11 at 09:31
  • getting an int like is at best not portable. For example, it doesn't take endianness into consideration. – Jean-Denis Muys Sep 07 '11 at 12:12
  • Assuming the first 4 bytes form an integer (IOW, an integer was stored there), *(int *)n will produce that int, no matter the endianness. Assembling it from the bytes would have to take endianness into consideration. – Rudy Velthuis Sep 07 '11 at 12:43
0
int n ; // first n bytes
NSData *data; // your data

NSData *subData = [data subdataWithRange:NSMakeRange(0, n)]; // make sure if data has n bytes

NSString *stringData = [subData description];
stringData = [stringData substringWithRange:NSMakeRange(1, [stringData length]-2)];

unsigned dataAsInt = 0;
NSScanner *scanner = [NSScanner scannerWithString: stringData];
[scanner scanHexInt:& dataAsInt];
deepax11
  • 199
  • 3
  • 7