#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Convert char string to HEX format


    Hi pros,

    Being a beginner in coding in C, need to seek help here:

    I have a string in char string and I wish to have it in hex format but dunno how.

    For eg:

    char key[]="ABCDEF0102";
    and I wish to convert the string to a hex format i.e. key[0]="0xAB", key[1]="0xCD", key[3]="0x01" etc..

    Can you advise how this can be achieve? thanks a million!
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1807
    Do you really want to do that? What is the point? The string could already be regarded as being hex.

    Your code makes little sense since key[0] for example is a single character byte the literal string constant "0xAB" is five characters.

    In short it is not exactly clear what you want to do. And the literal interpretation of what you have asked seems unlikely I am reluctant to explore it without further clarification.

    For example fo you really want key[] to be transformed into a series strings with 0x prefixes, or did you instead mean that you wanted to convert key[] into eight bit integers where each byte is the integer conversion of the hex string? The first is not a problem of 'conversion' but simply string manipulation, the second is conversion. You mentioned 'conversion', but you described string manipulation.

    Perhaps this covers both bases and will give you some ideas:

    C Code:
     
    #include <stdio.h>
    #include <string.h>
     
    int main()
    {
        char key[]="ABCDEF0102" ;
        char hexbyte[3] = {0} ;
        int octets[sizeof(key) / 2] ;
     
        for( int d = 0; d < strlen(key); d += 2 )
        {
            // Assemble a digit pair into the hexbyte string
            hexbyte[0] = key[d] ;
            hexbyte[1] = key[d+1] ;
     
            // Convert the hex pair to an integer
            sscanf( hexbyte, "%X", &octets[d/2] ) ;
     
            // Show the integer has a hex pair with prefix
            printf( "0x%2.2X\n", octets[d/2] ) ;
        }
     
        return 0 ;
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0
    Hi, thanks for your reply. Sorry that I might not have been clear in my description. Let me try again.

    Yup, I guess you are right. I needed a string manipulation instead of 'conversion'.

    I have some data from a textfile which i needed to extract, which is why I had them stored in an array key[]="ABCDEF0102" for example. But since they were extracted as a 'char' , I needed these data in bytes (i.e. "0xAB","0xCD") instead but have no idea how to go about doing that.

    Or is there a way where by I can extract them as bytes instead of chars?
  6. #4
  7. Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jun 2005
    Posts
    5,929
    Rep Power
    4856
    Take 'A', for instance. Suppose you want to wind up with binary value, 10. If the 'A' is encoded as ASCII, it would have the value, 0x41. That isn't 10. If it were encoded in Unicode or some other method of encoding characters, it would have some other value, perhaps even more than one byte.

    Conversion is a matter of mapping. Sometimes the mapping is regular enough to be amenable to algorithmic extraction, sometimes not.

    You have to know what you want, precisely, and what you have to work with. We can't help you with that part.

    If you're not interested in conversion, but merely want to transform 'A' to "0x0A", then it's merely a matter of prepending additional characters, however they're encoded.

    Put your feet up, clasp your hands behind your head, and have a little thought time.
    Write no code whose complexity leaves you wondering what the hell you did.
    Politically Incorrect DaWei on Pointers Grumpy on Exceptions
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1807
    Originally Posted by musicroamer
    Or is there a way where by I can extract them as bytes instead of chars?
    It remains unclear what you want. You say
    I needed these data in bytes
    and then precede to show examples of strings
    (i.e. "0xAB","0xCD")
    Either way the example I gave shows you how to do both. The printf() outputs the strings you asked for, if you use sprintf instead, you can store then elsewhare rather than displaying them. And the scanf() call illustrates how to convert hex strings to byte integers. It reads into an int rather than a char out of necessity - there is no format specifier type modifier for char sized integers, however that is easily solved:
    Code:
    char octet ;
    int x ;
    sscanf( hexbyte, "%X", &x ) ;
    octet = (char)x ;
    Clifford
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    integer to hexadecimal conversion - more information i need


    Hello all...
    Thanks for this thread.
    I would like to get little more information in thsi thread.
    I have a number say unsigned int a = 2010.
    I need to convert it to its hexadecimal for say hexnumber wich i can use for further calculation.

    say hexadecimal form of 2010 is stored in hexnumber.
    I need to use it to find an address place p(0xhexnumber).

    Hope i gave you clear idea of my problem. Please help me.

    thanks,
    Divya
  12. #7
  13. Who set my Title?
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Posts
    715
    Rep Power
    262
    Why did you have to hijack an old thread? You need to make your own topic for your questions.

    Your question does not make sense anyway - your number is stored as bits and you can interpret it as decimal or hex number. The value would be the same...

    This is what you want:
    Code:
    int hexnumber = a;
    There, your conversion is done!
    Nobody is perfect. I am Nobody.

IMN logo majestic logo threadwatch logo seochat tools logo