CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 1999
    Location
    Philadelphia, USA
    Posts
    195

    Converting Hex Characters to readable ASCII

    When you receive a GET from a web browser, the parameters come encoded in Hex character format. Is there an MFC function to convert the hex characters to readable ascii characters, or do I have to manually write a switch() statement to parse the hex chars into ascii? If so, anyone already written one?

    Thanks.

    www.DummySoftware.com
    ============================
    Tired of popup windows while you surf the web?
    Get PopupDummy! http://www.popupdummy.com

  2. #2
    Join Date
    Feb 2000
    Location
    Uddevalla, Sweden
    Posts
    183

    Re: Converting Hex Characters to readable ASCII

    some error checking may be added ( excuse typos or it doesn't work
    but you could do something like this :

    use:


    unsigned char HexToChar( char * ptrtohex );

    int main()
    {
    char hex[] = "AB";
    printf("%s is %d", HexToChar( hex ) );
    }

    unsigned char HexToChar( char * ptrtohex )
    {
    unsigned char ret;
    if( ptrtohex[0] >= 'A' && ptrtohex[0] <= 'F' )
    ret = (ptrtohex[0]-'A' + 10) << 4;
    else if( ptrtohex[0] >= 'a' && ptrtohex[0] <= 'f' )
    ret = (ptrtohex[0]-'a' + 10) << 4;
    else if( ptrtohex[0] >= '0' && ptrtohex[0] <= '9' )
    ret = (ptrtohex[0]-'0') << 4;

    if( ptrtohex[1] >= 'A' && ptrtohex[1] <= 'F' )
    ret = ret | ( ptrtohex[1]-'A' +10 );
    else if( ptrtohex[1] >= 'a' && ptrtohex[1] <= 'f' )
    ret = ret | ( ptrtohex[1]-'a' +10);
    else if( ptrtohex[1] >= '0' && ptrtohex[1] <= '9' )
    ret = ret | (ptrtohex[1]-'0');

    return ret;
    }







  3. #3
    Join Date
    Sep 1999
    Location
    Philadelphia, USA
    Posts
    195

    Re: Converting Hex Characters to readable ASCII

    I compiled it up real quick and got an Unhandled Exception.
    What exactly are you passing in there - a single character? Or a set of
    characters that make up a hex character to be converted into an ascii character?

    When the browser passes you the string with all those hex characters, you would need to send something like 2 or 3 characters to your function for it to convert it. Something like :

    Here+is+some+stuff%0D%0A

    Where the %0D is a \r
    %0A is a \n

    And there are 3 characters in total. Any ideas on modifying that code?


    www.DummySoftware.com
    ============================
    Tired of popup windows while you surf the web?
    Get PopupDummy! http://www.popupdummy.com

  4. #4
    Join Date
    Sep 1999
    Location
    Philadelphia, USA
    Posts
    195

    Re: Converting Hex - Final String Solution

    Hey, I got it working.. works great! Thanks a lot. Here is some extra work done to it to perfectly convert a web GET query (including spaces) with hex characters to a full string using the previous HexToChar function, in case anyone else out there has this problem:


    // Help function converts a Hex character consisting
    // of 2 characters ptrtohex[2] to an ASCII character.
    unsigned char HexToChar( char * ptrtohex )
    {
    unsigned char ret;
    if( ptrtohex[0] >= 'A' && ptrtohex[0] <= 'F' )
    ret = (ptrtohex[0]-'A' + 10) << 4;
    else if( ptrtohex[0] >= 'a' && ptrtohex[0] <= 'f' )
    ret = (ptrtohex[0]-'a' + 10) << 4;
    else if( ptrtohex[0] >= '0' && ptrtohex[0] <= '9' )
    ret = (ptrtohex[0]-'0') << 4;

    if( ptrtohex[1] >= 'A' && ptrtohex[1] <= 'F' )
    ret = ret | ( ptrtohex[1]-'A' +10 );
    else if( ptrtohex[1] >= 'a' && ptrtohex[1] <= 'f' )
    ret = ret | ( ptrtohex[1]-'a' +10);
    else if( ptrtohex[1] >= '0' && ptrtohex[1] <= '9' )
    ret = ret | (ptrtohex[1]-'0');

    return ret;
    }

    // Converts a CString (usually obtainined through a GET query to the browser
    // and converts any %Hex characters or + spaces
    // into its ASCII equivelent and returns a CString.
    CString HexToString(CString strDecoded)
    {
    int ndx = 0;

    // change all '+' to ' '
    while( (ndx=strDecoded.Find('+')) != -1 )
    strDecoded = strDecoded.Left(ndx) + ' ' + strDecoded.Mid(ndx+1);

    // first see if there are any %s to decode....
    if ( strDecoded.Find( '%' ) != -1 )
    {
    // iterate through the string, changing %dd to special char....
    for( ndx=0; ndx < strDecoded.GetLength(); ndx++ )
    {
    char ch = strDecoded.GetAt( ndx );
    if ( ch == '%' )
    {
    if ( strDecoded.GetAt( ndx+1 ) == '%' )
    {
    // wanna keep one percent sign....
    strDecoded = strDecoded.Left(ndx) + strDecoded.Mid(ndx+1);
    }
    else
    {
    // assume we have a hex value....
    char ch1 = strDecoded.GetAt(ndx+1);
    char ch2 = strDecoded.GetAt(ndx+2);
    char chs[3];
    chs[0] = ch1;
    chs[1] = ch2;
    CString oldstr = strDecoded;
    strDecoded = strDecoded.Left(ndx);
    strDecoded += HexToChar((char*)chs);
    strDecoded += oldstr.Mid(ndx+3);
    }
    }
    }
    }

    return strDecoded;
    }




    www.DummySoftware.com
    ============================
    Tired of popup windows while you surf the web?
    Get PopupDummy! http://www.popupdummy.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured