Convert ascii char[] to hexadecimal char[] in C

129,291

Solution 1

#include <stdio.h>
#include <string.h>

int main(void){
    char word[17], outword[33];//17:16+1, 33:16*2+1
    int i, len;

    printf("Intro word:");
    fgets(word, sizeof(word), stdin);
    len = strlen(word);
    if(word[len-1]=='\n')
        word[--len] = '\0';

    for(i = 0; i<len; i++){
        sprintf(outword+i*2, "%02X", word[i]);
    }
    printf("%s\n", outword);
    return 0;
}

Solution 2

replace this

printf("%c",word[i]);

by

printf("%02X",word[i]);

Solution 3

Use the %02X format parameter:

printf("%02X",word[i]);

More info can be found here: http://www.cplusplus.com/reference/cstdio/printf/

Share:
129,291
Biribu
Author by

Biribu

Updated on September 19, 2020

Comments

  • Biribu
    Biribu over 3 years

    I am trying to convert a char[] in ASCII to char[] in hexadecimal.

    Something like this:

    hello --> 68656C6C6F

    I want to read by keyboard the string. It has to be 16 characters long.

    This is my code now. I don't know how to do that operation. I read about strol but I think it just convert str number to int hex...

    #include <stdio.h>
    main()
    {
        int i = 0;
        char word[17];
    
        printf("Intro word:");
    
        fgets(word, 16, stdin);
        word[16] = '\0';
        for(i = 0; i<16; i++){
            printf("%c",word[i]);
        }
     }
    

    I am using fgets because I read is better than fgets but I can change it if necessary.

    Related to this, I am trying to convert the string read in a uint8_t array, joining each 2 bytes in one to get the hex number.

    I have this function which I am using a lot in arduino so I think it should work in a normal C program without problems.

    uint8_t* hex_decode(char *in, size_t len, uint8_t *out)
    {
        unsigned int i, t, hn, ln;
    
        for (t = 0,i = 0; i < len; i+=2,++t) {
    
                hn = in[i] > '9' ? (in[i]|32) - 'a' + 10 : in[i] - '0';
                ln = in[i+1] > '9' ? (in[i+1]|32) - 'a' + 10 : in[i+1] - '0';
    
                out[t] = (hn << 4 ) | ln;
                printf("%s",out[t]);
        }
        return out;
    

    }

    But, whenever I call that function in my code, I get a segmentation fault.

    Adding this code to the code of the first answer:

        uint8_t* out;
        hex_decode(key_DM, sizeof(out_key), out);
    

    I tried to pass all necessary parameters and get in out array what I need but it fails...

  • Biribu
    Biribu almost 11 years
    I am printing just for example. But I want to save the string in other string. How do I have to do the cast?
  • BLUEPIXY
    BLUEPIXY almost 11 years
    @Biribu the results of this program stored in outword. Casting is not necessary.
  • Biribu
    Biribu almost 11 years
    I saw it right now. Sorry. I just saw the printf when I read. It works fine. Thank you so much.
  • Biribu
    Biribu almost 11 years
    I added another question related to it. Do you know why it fails?
  • S Andrew
    S Andrew about 7 years
    @BLUEPIXY what does outword+i*2 means.?
  • BLUEPIXY
    BLUEPIXY about 7 years
    @SAndrew It means the position to write out
  • S Andrew
    S Andrew about 7 years
    @BLUEPIXY so if i=0, it will write at outward[0], if i=1, then outward[2], if i=2, then outward[4].. is this correct.?
  • S Andrew
    S Andrew about 7 years
    @BLUEPIXY I dont think its right because in my case it saves properly at index 0,1,2,3... According to your c code in answer, my outward is of 255 and word is also of 255 in size.
  • BLUEPIXY
    BLUEPIXY about 7 years
    @SAndrew char word[17], outword[33];//17:16+1, 33:16*2+1. i don't know your case.