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

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...

This question is related to c hex ascii

The answer is


Use the %02X format parameter:

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

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


replace this

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

by

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

void atoh(char *ascii_ptr, char *hex_ptr,int len)
{
    int i;

    for(i = 0; i < (len / 2); i++)
    {

        *(hex_ptr+i)   = (*(ascii_ptr+(2*i)) <= '9') ? ((*(ascii_ptr+(2*i)) - '0') * 16 ) :  (((*(ascii_ptr+(2*i)) - 'A') + 10) << 4);
        *(hex_ptr+i)  |= (*(ascii_ptr+(2*i)+1) <= '9') ? (*(ascii_ptr+(2*i)+1) - '0') :  (*(ascii_ptr+(2*i)+1) - 'A' + 10);

    }


}

Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to hex

Transparent ARGB hex value How to convert a hex string to hex number Javascript: Unicode string to hex Converting Hexadecimal String to Decimal Integer Convert string to hex-string in C# Print a variable in hexadecimal in Python Convert ascii char[] to hexadecimal char[] in C Hex transparency in colors printf() formatting for hex Python Hexadecimal

Examples related to ascii

Detect whether a Python string is a number or a letter Is there any ASCII character for <br>? UnicodeEncodeError: 'ascii' codec can't encode character at special name Replace non-ASCII characters with a single space Convert ascii value to char What's the difference between ASCII and Unicode? Invisible characters - ASCII How To Convert A Number To an ASCII Character? Convert ascii char[] to hexadecimal char[] in C Convert character to ASCII numeric value in java