views:

416

answers:

2

Are there any Cocoa classes that will help me convert a hex value in a NSString like 0x12FA to a long or NSNumber? It doesn't look like any of the classes like NSNumberFormatter support hex numbers.

Thanks, Hua-Ying

+6  A: 

See NSScanner's scanHex...: methods. That'll get you the primitive that you can wrap in an NSNumber.

Joshua Nozzi
+10  A: 

Here's a short example of how you would do it using NSScanner:

NSString* pString = @"0xDEADBABE";
NSScanner* pScanner = [NSScanner scannerWithString: pString];

unsigned int iValue;
[pScanner scanHexInt: &iValue];
ttvd