I've an NSData object, First I want to convert NSData object to bytes, then read the first four bytes in this NSData object & 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?
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:
RTFM the Byte ordering and byte swapping sections of the Memory Management Programming Guide for Core Foundation.
That's quite simple. Use
bytes
to get at the bytes and then cast tounsigned char*
Update
To turn this into a single
int
assumesbytes
contains a validint
: