My goal is to compare two hex strings and determine which number is higher. I assume I need to convert those hex strings to integers to be able to compare them mathematically, but the conversion to unsigned
isn't working. Here's what I've tried:
NSString *firstHex = @"35c1f029684fe";
NSString *secondHex = @"35c1f029684ff";
unsigned first = 0;
unsigned second = 0;
NSScanner *firstScanner = [NSScanner scannerWithString:firstHex];
NSScanner *secondScanner = [NSScanner scannerWithString:secondHex];
[firstScanner scanHexInt:&first];
[secondScanner scanHexInt:&second];
NSLog(@"First: %d",first);
NSLog(@"Second: %d",second);
But the log output gives me:
First: -1
Second: -1
I can't figure out what I'm doing wrong. Am I using NSScanner
correctly here? Thanks in advance.
Your hex numbers are 13 digits long - 52 binary bits. This is longer than 32 bits, use long long
variables and scanHexLongLong:
instead.