I've an NSData object, the length is 4 bytes .These four bytes i'm extracting from another NSData object using ,
fourByteData=[completeData subdataWithRange:NSMakeRange(0, 16)];
My first question is, will the above statement provide me the first four bytes of complete data.
If Yes, then how to convert all these bytes to an equivalent int.
iOS integer stores LSB (lowest significant byte) in the first byte and MSB in the last byte. I have conversion routine to test all those things. check here,
Test ...
Util.m
It depends on the Endianness notation of the data you want to convert, in relation to your device Endianness notation. wiki on Endianness
To keep it simple you need to check does two method
or
And check which one make more sense when you parse the data.
Assuming
_vertexData
isNSData
here and you know what data (types) to expect in your buffer you can iterate thru this block with NSData's.length
property. In this Example each data block was 32 Bytes (storing 8 x float values) and i was interested in logging starting at the 5th float valuethis could have been written much shorter, but for the sake of clarity and with less use of miss leading castings
maybe this helps someone
That statement would give you the first 16 bytes of data, not 4. To get the first 4 bytes you need to modify your statement to:
To read the data from the NSData Object to an integer you could do something like:
You do not need to get the first 4 bytes if you are just converting it to an integer. You can do this directly from the two lines above and your
completeData
receiverNo you will get 16 bytes of data, since the range is from offset 0 and then 16 bytes.
If you had a
NSData
instance with 4 bytes then you could do a simple type cast like this: