3

I have a NSData object with 4 bytes length. I know this 4 bytes construcuts a positive integer. How should I get the int from those NSdata object?

Charith Nidarsha
  • 4,195
  • 3
  • 28
  • 32
  • Is the 4-byte value a 32-bit number, or 4 ASCII digits? E.g. would 4321 be 0x000010E1 (32-bit big-endian number) or 0x34333231 ("4321"in ASCII). If it's a number, is it signed (int32_t) or unsigned (uint32_t)? Is it big endian or little endian (or same as the running system)? – Matthew Flaschen Jun 23 '10 at 04:05
  • I have found the solution here. char buffer[4]; [data getBytes:buffer length:4]; int dataSize = 0; char cBuf2[4]; for(int k=0;k < 4; ++k) { cBuf2[k] = buffer[3-k]; } memcpy(&dataSize, cBuf2, 4); NSLog(@"Zip stream size :%d", dataSize); – Charith Nidarsha Jun 23 '10 at 09:09

3 Answers3

12

If you know the endianness matches the current system:

x = *(const UInt32 *)[theData bytes];

If you know the endianness is backwards, follow that with:

x = Endian32_Swap( x );

If you are not sure of the source and target endianness, you should probably find another way to pass the data.

drawnonward
  • 53,459
  • 16
  • 107
  • 112
  • 2
    You can use `EndianU32_BtoN` (or `EndianS32_BtoN` for signed) if you know the source is big-endian, and `EndianU32_LtoN` if you know it's little-endian. This means you don't have to know the endianness of the running system. – Matthew Flaschen Jun 23 '10 at 04:00
  • - (void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { //I know this data is 4bytes length NSString *msgReceived = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; //I want to get the int from the NSData } – Charith Nidarsha Jun 23 '10 at 04:23
3
int32_t value; // make unsigned if needed. 
[myData getBytes: &value];
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 1
    `getBytes:` is deprecated in OS X 10.6 and iPhone OS 4.0, so one may use [`getBytes:length:`](http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/instm/NSData/getBytes:length:) to avoid warnings. – dreamlax Jun 23 '10 at 03:58
  • Thanks for reply. but this return a huge integer value like 865465934. I have stuck with this for almost 1 day now. – Charith Nidarsha Jun 23 '10 at 04:00
  • 2
    @charith, it sounds like the endianness is wrong, try swapping it with [`NSSwapLittleIntToHost`](http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSwapLittleIntToHost) – dreamlax Jun 23 '10 at 04:03
  • @charith, see my comment above. It will help if you give more info. – Matthew Flaschen Jun 23 '10 at 04:07
  • @dreamlax, I'm trying. I will let you know the results for sure. Thanks again. – Charith Nidarsha Jun 23 '10 at 04:11
  • - (void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { //I know this data is 4bytes length NSString *msgReceived = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; //I want to get the int from the NSData } – Charith Nidarsha Jun 23 '10 at 04:23
1

I have found the solution here.

char buffer[4]; 

[data getBytes:buffer length:4]; 

int dataSize = 0; 

char cBuf2[4]; 

for(int k=0;k < 4; ++k) {

 cBuf2[k] = buffer[3-k]; 

} 

memcpy(&dataSize, cBuf2, 4); 

NSLog(@"Zip stream size :%d", dataSize);
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Charith Nidarsha
  • 4,195
  • 3
  • 28
  • 32