Reading bytes from a bin file and storing into a string with descriptor
I'm trying to get a string containing the value of the 4 bytes at position 424 as they appear in my Hex editor: my hex editor displays 14583212 and I need a string containing "14583212"
This is the code that doesen't return anything but 000000:
- (NSString *) tcExtract: (NSString *) nomefile {
NSFileHandle *myFile= [NSFileHandle开发者_如何学Python fileHandleForReadingAtPath:nomefile];
[myFile seekToFileOffset:424];
NSData *myData= [myFile readDataOfLength:4];
NSString *maxData= [NSString stringWithFormat:@"%04x", [myData description]];
What am I doing wrong?
Thanks
The problem is in your call to [myData description]
. That's giving you a string representing the hex value of the bytes. Instead, get the bytes themselves into an integer, then format that as a string:
NSData *myData= [myFile readDataOfLength:4];
int myInt;
[myData getBytes:&myInt length:4];
NSString *maxData= [NSString stringWithFormat:@"%04d", myInt];
精彩评论