In my objective-C code, I received a file containing hexadecimal values. I read that file and convert the first few bytes to an NSArray which I then try to read to perform computations on. The conversion seems to work, this is what it looks like:
NSBundle *mainBundle = [NSBundle mainBundle];
NSFileHandle *fh;
NSData *wordBuffer;
NSMutableArray *myByteArray = [NSMutableArray new];
NSString *filePath = [mainBundle pathForResource:@"data" ofType:@"txt"];
fh = [NSFileHandle fileHandleForReadingAtPath: filePath];
while ((wordBuffer = [fh readDataOfLength:1]) && [wordBuffer length] )
{
[myByteArray addObject:[self hexStringForData:wordBuffer]];
}
return myByteArray];
}
The problem is when I read back from that Array, values that use letter in hexadecimal are completely wrong, as if objective-C ignored the letters. FF returns 0, 7b returns 7 and so on.
So I get the myByteArray back from the init function, store it in response and try to read it like this:
unsigned long test = [response[1] unsignedLongValue];
NSLog(@"Value in unsigned long: %lu", test);
This crashed the app because of an NSInvalidArgumentException cause by the selector 'lu', but I don't understand why.
unsigned int test = [[answerHeader response][1] unsignedIntValue];
NSLog(@"Value in unsigned long: %u", test);
this returns 0
NSString *str = response[1];
unsigned long red = strtoul([str UTF8String], 0, 16);
NSLog(@"converted long: %lu", red);
this returns 255
I don't think I can really afford to read every single bytes like that (it looks more expensive that array[2] intValue). What's going on and how can I fix it ? Thanks in advance !