More like:

int StrHexToInt(const char* pszHex)
{
    int nResult = 0;                      // Result will go here
    for (int i = 0; *pszHex && (i < (sizeof(int) << 1)); i++)
    {                                     // While we have a string and
haven't exceeded int's precision...
        nResult <<= 4;                    // Shift up accumulated result
        char c = *pszHex++;               // Grab a HEX (text) digit
        if ((c & 0xF0) == 0x30)           // test the "zone"
            nResult |= (c & 0x0F);        // 0 - 9
        else
            nResult |= ((c & 0x0F) + 9);  // Assume [A - F] or [a - f]
    }
    return (nResult);
}

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Jason
Dawes
Sent: Monday, May 22, 2000 3:10 PM
To: Palm Developer Forum
Subject: Re: Converting an Hexa String to int


At 04:28 PM 5/22/00 +0200, you wrote:
>Is there, in Palm API, an efficient way to convert an Hexadecimal String
to the corresponding integer value ?
>If no, can someone show me a quick way to do that ?
>

Should be something like this:

// assumes s is a valid hex string containing an int with no leading 0x
int StrHToA(char * s)
{
    int i, n = 0;

    for(i=0;i<sizeof(int) && (1 || (n << 4));i++)
    {
        c = s[i];
        if (c & 0x0400) // A-Z or a-z
            c = (c & 0x000F) + 9;
        else    // assume it's 0-9
            c &= 0x000F;
        n |= c;
    }
    return n;
}


--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to