Convert hexadecimal NSString (0xff00) to decimal integer?
Convert hexadecimal NSString (0xff00) to decimal integer?
- Subject: Convert hexadecimal NSString (0xff00) to decimal integer?
- From: Greg Hurrell <email@hidden>
- Date: Wed, 8 Jan 2003 00:24:48 +1030
I was rather surprised today when I couldn't find a way of converting
from an NSString containing a hexadecimal representation of a number
eg. something like "0xff00"
to a decimal integer of that number eg. 65280.
NSString has an -intValue method which can return an int value from an
NSString containing a *decimal* representation of an integer. But it
won't work for hexadecimal representations.
I didn't want to "re-invent the wheel" so to speak, as this must surely
be a fairly common requirement. I searched around a bit, couldn't find
anything in Core Foundation to do it, nor any examples on the web, so
in the end I did indeed "re-invent the wheel".
So I made a category that extends NSString and adds a method called
-intValueFromHexadecimal. Now tell me if all that was a waste in
time... (although it was useful as a programming exercise for a learner
like me at least).
Perhaps it's bleedingly obvious and staring me in the face, but I
couldn't find any shortcuts in NSString or NSNumber that might do this
conversion in one hit... and I find that odd because Apple obviously
has code which does this (if you look at the way their code can parse
plists it's evident they have plenty of tricks for converting between
various types of strings and numbers etc).
Cheers :-)
Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.