What's the easiest way to parse binaries in Cocoa Touch?
Let's say I have a binary (generated by Java) containing a 32 bit int value. I am currently using the following Objective-C code to parse it:
NSString *path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"dat"];
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];
unsigned long intValue;
memcpy(&intValue, [[file readDataOfLength:4] bytes], 4);
intValue = NSSwapBigLongToHost(intValue);
[file closeFile];
My first question is to ask if this is the usual way of doing things on the iPhone, because I haven't found anything close to Java DataInputStream.
My second question has to do with the line of code with memcpy. I don't understand why the next part (more elegant, less "low-level") doesn't work instead:
[[file readDataOfLength:4] getbytes:&intValue];
I am getting a build warning:
'NSData' may not respond to '-getbytes:'
On execution I get:
'NSInvalidArgumentException', reason: '*** -[NSConcreteData getbytes:]: unrecognized selector sent to instance
+1
source to share