Re: -hexValue for NSString?
Re: -hexValue for NSString?
- Subject: Re: -hexValue for NSString?
- From: Michael Ash <email@hidden>
- Date: Mon, 23 Mar 2009 23:50:49 -0400
On Mon, Mar 23, 2009 at 9:25 PM, Dave Keck <email@hidden> wrote:
> I think this should do the trick:
>
> @implementation NSString (HexIntValue)
>
> - (unsigned int)hexIntValue
> {
>
> NSScanner *scanner;
> unsigned int result;
>
> scanner = [NSScanner scannerWithString: self];
>
> [scanner scanHexInt: &result];
>
> return result;
>
> }
>
> @end
>
> Note that I would expect -hexValue to be a method of NSNumber, that
> would return an NSString of the receiver's hex-value representation. I
> titled the method above 'hexValueInt' to more closely match NSString's
> -intValue, -floatValue, etc.
Unless you're certain that the strings you send this message to will
always contain a valid hex int representation, you'll also want to
check the return value from scanHexInt: and return some prearranged
value for "nothing good found". For example, -intValue returns 0 if
the string doesn't start with a valid integer representation. The
current code will *probably* return whatever junk was on the stack
(although -scanHexInt:'s docs don't say what it does with the
parameter in the event of a failure, so it could end up doing
something reasonable).
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden