How can i convert a Hexadecimal nu开发者_Python百科mber to a nsuinteger
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.
strtoul(s, NULL, 16);
Here's one solution which uses the libc
function sscanf()
,
精彩评论