Thread: How to change CString data to CString data(in Hex format)? (And change back)

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    How to change CString data to CString data(in Hex format)? (And change back)

    I have this below CString data (

    Code:
    CString m_str; 
    m_str += "CAST";
    I need to change m_str to hex CString data. So I use this below function

    Code:
    CString m_hexstr
    m_hexstr.Format("%02X", m_str);
    So I get m_hexstr = {"BC3E5C"}

    But I want to ask about how to change back from hex CString data {"BC3E5C"} to CSting data {"CAST"}.

    Thank you for your reply.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Here is a function that I've used for a while

    Code:
    #include <ctype.h>
    
    unsigned long atoh(char *ivalue){
        char *value = ivalue;  
        unsigned long rtrVal=0, i, end;
    
        if((strstr(ivalue, "0X") == ivalue) || (strstr(ivalue, "0x") == ivalue))
            value+=2;
    
    	for(end=0, i = 0; isxdigit(value[i]); i++, end++);
    
        for(i = 0; i < end; i++){
            word bwI = (end-1) - i;
            word iChr;
            char lChr = toupper(value[i]);
    
            if(lChr > 64 && lChr < 71)
                iChr = lChr-55;
            else if(lChr > 48 && lChr < 58)
                iChr = (lChr - 48);
            else
                iChr = 0;
            if(iChr > 0)
                rtrVal += iChr << (bwI << 2);
        }
    
        return rtrVal;
    }
    However, it is all c. But you should be able to throw using namespace std in there and be just fine.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Thank you for your answer master5001

    I think I get the wrong answer.

    Because "CAST" should result in 8 letters. (2 per ASCII character).
    I think Format does not work ok this way.

    master5001 I want to ask you about your function. Is that function convert from hex character to CString? Do you know how to change from CString to hex character.

    Thank you for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to change data format between unsigned char and CString?
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 02:49 PM
  2. Change data format from CString to int
    By ooosawaddee3 in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2002, 10:43 AM