• 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: Probably a silly question but...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Probably a silly question but...


  • Subject: Re: Probably a silly question but...
  • From: Steve Sims <email@hidden>
  • Date: Thu, 10 Jun 2004 15:33:47 -0400

On 10 Jun 2004, at 00:30, April Gendill wrote:

Ok, I have a string which is actually a hex number. how would I convert that hex number into an integer?

I realise you've received a few replies to this already, but I wanted to do something similar myself recently. I didn't actually want an integer though, rather I was after was to be able to get binary data from a string of hex and vice versa. I'm not very familiar with standard C APIs, but I found an Obj-C/Foundation API equivalent.

NSString has a "propertyList" call which will return an NSData item with data that's in the format "<abcdef12 3456>". You need an even number of hex characters for this to work, and propertyList will throw an exception if it doesn't like the string you give it, so enabling obj-C exceptions and using a @try, @catch block helps.

Similarly NSData has "description" which creates strings in that format. Adding leading < and trailing > characters to a string is of course trivial. Extracting an integer from NSData is pretty easy.

This route though is of course overkill if you're dealing with strings that only contain a single integer.

Steve
_______________________________________________
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.


References: 
 >Probably a silly question but... (From: April Gendill <email@hidden>)

  • Prev by Date: Re: Re-formatting NSString
  • Next by Date: Re: two things (newbie)
  • Previous by thread: Re: Probably a silly question but...
  • Next by thread: Re: Probably a silly question but...
  • Index(es):
    • Date
    • Thread