Question

In a .NET web application the BitConverter class is being used to convert Int32's into a byte array like so:

byte[] intBuffer = BitConverter.GetBytes(12345);

Now I am trying to read this back into an iPhone app. I have the bytes in an NSData with a length of 4 but I'm not sure how to continue because I can't figure out how to view the contents of the nsdata in the debugger console.

NSData *intBuffer = [NSData dataWithBytes:[buffer bytes] length:4];

How can I convert these 4 bytes to an objective-c int?

Edit:

Here is something I came up with which may or may not be on the right track. I'm not sure how to get a single byte value by index in an NSData.

- (int) extractSize:(NSData *)data {
    NSMutableString *size = [NSMutableString stringWithCapacity:0];

    for (int i = [data length] - 1; i <= 0; i--) {
        //[size appendFormat:@"%x", data[i]];
    }

    return strtol([size UTF8String], nil, 16);
}
Was it helpful?

Solution

int i;
[intBuffer getBytes:&i range:NSMakeRange(0, 4)];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top