tags:

views:

60

answers:

4

Hi,

How can i convert a Hexadecimal number to a nsuinteger

A: 

Here's one solution which uses the libc funtion sscanf(),

Abizern
+2  A: 
strtoul(s, NULL, 16);
newacct
+2  A: 

You can use NSScanner if your hex number is in an NSString:

NSScanner* scanner = [NSScanner scannerWithString:@"0xFF"];
unsigned int foo;
[scanner scanHexInt:&foo];
NSLog(@"Integer: %ld",foo);
scanner = [NSScanner scannerWithString:@"FF"];
[scanner scanHexInt:&foo];
NSLog(@"Integer: %ld",foo);

Admittedly, this doesn't scan directly to an NSUInteger, you'd need to cast the result, but it should be enough for your purposes.

Rob Keniger
Thanks it is done.
iSight
If I answered your question, you should mark the question as answered, this way more people will help you in future.
Rob Keniger
Sorry, i am new to this blog. But, will make it out in the future. Any way, thanx for your answer.
iSight
A: 

In Rob's code snippet above, the code is unnecessarily duplicated.

ThomasW