• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: -hexValue for NSString?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >-hexValue for NSString? (From: Jonathon Kuo <email@hidden>)
 >Re: -hexValue for NSString? (From: Dave Keck <email@hidden>)

  • Prev by Date: Re: Stop NSScrollView from catching scroll events.
  • Next by Date: Re: IB instantiating objects
  • Previous by thread: Re: -hexValue for NSString?
  • Next by thread: Simulating (or obtaining) menubar effect in a Cell
  • Index(es):
    • Date
    • Thread