3

I'm looking for a way to get an int value from a binary file. So lets say i have this file "myfile.dat" where from my PC i stored a lot of stuff... Now i need to read that file from my IPhone and show the data...

on the "myfile.dat" i have this (binary and all ints are little endian): 0-12: A signature string 13-16: An int number (note that length = 4)

So using NSData i know i can read from pos 13 to pos 16 and get those bytes... i can get the first 0-12 string correctly, but i cannot read pos 13-16 and convert it to an int value in Obj-C.... ;(

I have something like:

unsigned char bytes[length];
[_data getBytes:bytes range:NSMakeRange(offset, length)];
int32_t elem = OSReadLittleInt32(bytes, 0);

Now, im a newbie when it comes to Obj-C and C/C++... all my life i have been working with C# (sad)...

Can anyone help? THANKS IN ADVANCE!

1 Answer 1

4

Give this a try:

unsigned long bytes;
[_data getBytes: &bytes length: sizeof(unsigned long)];

NSLog(@"%i", NSSwapLittleLongToHost(bytes));
Sign up to request clarification or add additional context in comments.

3 Comments

I still get some wrong number: 544106784. Which is wrong.. the right number i'm looking for is 333.
Can you add the output of NSLog(@"data: %@", [_data description])?
Oh boy, you are going to hate me... i was storing the values in the wrong order (on the file)... So this method of yours works just fine. So thank you very much :) you rock!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.