• 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: Hex Conversion to bytes....
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hex Conversion to bytes....


  • Subject: Re: Hex Conversion to bytes....
  • From: Shawn Erickson <email@hidden>
  • Date: Mon, 29 Nov 2004 07:38:18 -0800


On Nov 28, 2004, at 6:03 PM, John Draper wrote:

Hi,

How is the best way to take a series of hex characters like this...
8d6f123e67cd548ca24d2323cd4a6514

Then convert them into 8 bit bytes and place them into an array of
8 bit bytes.  I DONT mean a NSArray,  but just to place them
into memory,  which when dumped,  would look like this...

8d 6f 12 3e  <etc>

I did the usual searches for "string conversions",  "format" and
I don't see any utilities to do this.

Well a good old (ugly) C way follows... (with assumptions noted)

#define ATOIXLATE ('A' - '9' - 1)

//decode printable character stream to RUID byte stream
for (ruid_i = 0, string_i = 0; string_i < string_size;
     ruid_i++, string_i +=2)
{
    /* In brief, each byte of the RUID is
    represented by two characters
    in hex notation. ('0' through '9' and 'A'
                      through 'F'). If a character
    is outside this range, it'll get caught by
    the checksum compare.

    So, you can get from '0' to 0, '1' to 1, ...
    '9' to 9 by subtracting
    '0' from the character. But with 'A' to 'F',
    you have to subtract
    ('0' + another constant), with this other
    constant being ATOIXLATE.
    So this line of code says, "Take first
character of byte, if it's
a hex digit, then subtract the constant, then
subtract '0' and
there's the top nibble. Shift it over then
'or' in the bottom
nibble by doing the same thing to it.
*/

    pcRuid[ruid_i] = (
        (((((pString[string_i] > '9') ?
        (pString[string_i] - ATOIXLATE) :
        (pString[string_i]) ) - '0')  & 0xf) << 4 )
        |
	((((pString[string_i + 1] > '9') ?
        (pString[string_i + 1] - ATOIXLATE) :
        (pString[string_i + 1]) ) - '0' ) & 0xf )
        );

    //only hit if string is corrupt, has odd number of characters
    if (pString[string_i + 1] == 0)
        break;
}


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Hex Conversion to bytes.... (From: John Draper <email@hidden>)

  • Prev by Date: Re: My bindings-enabled NIB ate itself... ? [No, I killed it myself....]
  • Next by Date: Re: IB inspector won't load [Really Solved] (Accessorizer updated)
  • Previous by thread: Re: Hex Conversion to bytes....
  • Next by thread: Re: Hex Conversion to bytes....
  • Index(es):
    • Date
    • Thread