Joshua Weinberg's answer is mostly correct, however the Ox prefix is optional when scanning hexadecimal integers. If you have a string in the format 01FFFFAB you can still use an NSScanner but you can simply skip the first character.
Up vote 13 down vote favorite 3 share g+ share fb share tw.
I would like to know how to parse a hex string, representing a number, in objective c. I am willing to use both an objective, or a C based method, either is fine. Example: #01FFFFAB should parse into the integer: 33554347 Any help would be appreciated!
Iphone objective-c string parsing hex link|improve this question asked Sep 6 '10 at 0:29Richard J. Ross III10.7k22053 98% accept rate.
Joshua Weinberg's answer is mostly correct, however the Ox prefix is optional when scanning hexadecimal integers. If you have a string in the format #01FFFFAB, you can still use an NSScanner, but you can simply skip the first character. Unsigned result = 0; NSScanner *scanner = NSScanner scannerWithString:@"#01FFFFAB"; scanner setScanLocation:1; // bypass '#' character scanner scanHexInt:&result.
1 Whoops, my bad, misread that in the docs. – Joshua Weinberg Sep 6 '10 at 3:43 1 Thanks, exactly what I needed! – Richard J.
Ross III Sep 6 '10 at 13:22.
You can use NSScanner for this int outVal; NSScanner* scanner = NSScanner scannerWithString:@"0x01FFFFAB; scanner scanHexInt:&outVal; outVal will contain the int you're looking for. The 0x is optional.
In addition, you could use a string substitution to convert the # character to the 0x prefix. – hotpaw2 Sep 6 '10 at 0:57 should be unsigned int outVal; – wanghq Sep 1 '11 at 6:19.
Strtol() is your friend. It converts a string to a long, and you can pass the base of the number in. Strip that # sign off first though, or pass to strtol a pointer to the first numerical character.
I'm going with this method, way simpler (and potentially quicker) than allocating an nsscanner. – Chris Jun 1 '11 at 6:25 @Chris: It's probably no quicker if you have an NSString, because you'd need to convert the NSString to a C string before you can use strtol. If you already have the text in a C string, it will probably be faster, but unless you are doing this thousands or millions of times I doubt you'd feel the performance hit.
– dreamlax Dec 6 '11 at 21:30.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.