0

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 !

Tom
  • 163
  • 1
  • 11
  • The invalid selector is likely `unsignedLongValue` and not `lu`, isn't it? Judging by the name of `hexStringForData:`, it's probably returning an `NSString *`, and it does not have an `unsignedLongValue`, only [`intValue`](https://developer.apple.com/documentation/foundation/nsstring/1414988-intvalue?language=objc) and `longLongValue`. – DarkDust Jul 07 '22 at 15:34
  • Probably related: [Convert Hex to ASCII number on Objective-c](https://stackoverflow.com/questions/18827699/convert-hex-to-ascii-number-on-objective-c) – DarkDust Jul 07 '22 at 15:36
  • @DarkDust thanks for clearing this out for me, I'm just starting to understand how to deal with elements from NSArrays. I think there's no way around a string to hex conversion then, right ? – Tom Jul 07 '22 at 15:58
  • 1
    So you have a file that just contains hex numbers, and in the end you want to convert it to data (`NSData`), right? Luckily lots of people have already done this before and there's multiple questions and answers already here. See for example: [Converting HEX NSString To NSData](https://stackoverflow.com/questions/7317860/converting-hex-nsstring-to-nsdata) and [How to get NSData From Hex String?](https://stackoverflow.com/questions/42198897/how-to-get-nsdata-from-hex-string). – DarkDust Jul 07 '22 at 16:11
  • Try to read hex chars by 2 : 1 byte is displayed with 2 chars. – Ptit Xav Jul 07 '22 at 18:50
  • @PtitXav, thanks, but I think I'm reading them in the first code snippet that stores it in myByteArray works, when I print it it follows the result of calling hexdump on the file. It's just converting it back to unsigned integer that's problematic – Tom Jul 08 '22 at 07:30

1 Answers1

0

I would recommend you to read the file into string: read txt file into string

NSStringEncoding encoding;
NSError *error;
NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:filePath
                                                      usedEncoding:&encoding
                                                             error:&error]

then parse hex to int

unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:@"#01FFFFAB"];

[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&result];
Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42