On Mon, Mar 23, 2009 at 9:25 PM, Dave Keck <[email protected]> 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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to