you need to take 2 (hex) chars at the same time... then calculate the int value and after that make the char conversion like...

char d = (char)intValue;

do this for every 2chars in the hex string

this works if the string chars are only 0-9A-F:

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

int hex_to_int(char c){
        int first = c / 16 - 3;
        int second = c % 16;
        int result = first*10 + second;
        if(result > 9) result--;
        return result;
}

int hex_to_ascii(char c, char d){
        int high = hex_to_int(c) * 16;
        int low = hex_to_int(d);
        return high+low;
}

int main(){
        const char* st = "48656C6C6F3B";
        int length = strlen(st);
        int i;
        char buf = 0;
        for(i = 0; i < length; i++){
                if(i % 2 != 0){
                        printf("%c", hex_to_ascii(buf, st[i]));
                }else{
                        buf = st[i];
                }
        }
}
Answer from sharpner on Stack Overflow
🌐
GitHub
github.com › ProgrammingSimpleSteps › c-examples › blob › main › number-systems › hex-to-ascii › hex-to-ascii.c
c-examples/number-systems/hex-to-ascii/hex-to-ascii.c at main · ProgrammingSimpleSteps/c-examples
Examples in C from c-programming-simple-steps.com's tutorials - c-examples/number-systems/hex-to-ascii/hex-to-ascii.c at main · ProgrammingSimpleSteps/c-examples
Author: ProgrammingSimpleSteps
🌐
Agnostic Development
agnosticdev.com › blog-entry › c › testing-and-converting-hexadecimal-data-c
Testing and Converting Hexadecimal Data in C | Agnostic Development
November 24, 2017 - From here I created three character buffers, one for the reverseBuffer, that we will use to reverse engineer our decimal value, one for the swapBuffer that we will use to swap our hex data into ASCII, and the last for the compareBuffer to compare the reverse engineered value to our sprintf swap buffer to get the results of our tests.
Top answer
1 of 2
2

What am I missing here?

To be honest, the code is so far from plausible, that I am going to ignore that question and the code and just present a solution.

// Convert single Hex digit to integer 0-15
int HexNibbleToInt( char nibble )
{
    return nibble > '9' ? 
           nibble - 'A' + 10 : 
           nibble - '0' ;
}

// Convert string of ASCII hex digit pairs representing 
// character codes to character string.
size_t StrToHex( const char strIn[], char strOut[] )
{
    int i = 0 ;
    for( i = 0; strIn[i * 2] != 0; i++)    
    {
        strOut[i] = HexNibbleToInt( strIn[i * 2] ) << 4 |
                    HexNibbleToInt( strIn[i * 2 + 1] ) ;
    }
    strOut[i] = 0 ;
    
    return i;
}

Note that I have omitted the strLen parameter. If it is a pre-condition that the strIn is a null terminated string with an even number of characters, then then you do not need to calculate the length. Since strlen() has to iterate the string to find the NUL and you have to iterate the string in any case, it serves no purpose. Of course a malformed string that is not capitalised hex digit pairs will have undefined behaviour. That is easy to handle, but if the string has already been validated you might not want to spend CPU cycles doing it twice.

Note also that I have returned the length of the output which saves yet another unnecessary and potentially expensive call to strlen():

size_t output_len = StrToHex( hexbuffer, charbuffer ) ; 
HAL_UART_Transmit( &huart3, charbuffer, output_len, 1000);

Moreover as this appears to be targeted at embedded code, I have avoided "expensive" functions such as sprintf() and strtoul(). If you are using them extensively already in the code, then there is probably little marginal cost in using them, but the initial cost of invoking them once may be rather high.

2 of 2
0

You're not doing anything to parse the hex. sprintf() doesn't do any parsing. You can use strtol() to parse a numeric string in a particular base.

You don't need to use sprintf() to write a single character to an array, just assign the value directly to the array element.

You need to make c a null-terminated string, so add '\0' to it, not '\n'. You don't actually need to append this explicitly, since you initialized the array to all zeroes.

Finally, you need to add a null terminator to strOut.

void Str2Hex(char strIn[],char strOut[], size_t strLen)
{
    for(uint16_t i = 0; i < strLen; i += 2)
    {
        uint8_t c[3] = {0};
        c[0] = strIn[i];
        c[1] = strIn[i+1];
        long ascii = strtoul(c, NULL, 16);
        strOut[i/2] = ascii;
    }
    strOut[strLen/2] = '\0';
}
Top answer
1 of 1
2

The statement

temp = (*(input)) >> (4*(i-1));

could be rewritten as

uint8 x = *(input);
temp = x >> (4 * (i - 1));

or

temp = input[0] >> (4 * (i - 1));

And now you can see that you are actually shifting the same value for 0, 4, 8, 12,... bits to the right. And when shifting value to the right, you are padding 0's from the left side, therefore after 2 iterations of the loop, your temp variable is always 0.

The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined. - ISO/IEC 9899:TC3, Section 6.5.7: Bitwise shift operators

You need to increment your input pointer. However in your code, you would need to repeat the code twice for every byte - for lower and upper 4 bits of the uint8.

This is how I would do it (replaced macro with inline function as pointed out by Olaf in the comment):

/*! \brief Convert nibble (lower 4 bits) to HEX value to avoid using standard libraries.
 */
static inline __attribute__((always_inline, const)) char
NibbleToHex(uint8_t nibble)  {
    return ((nibble <= 9) ? ('0' + nibble) : ('A' + nibble - 10));
}

static void HexToAscii(const uint8_t *input, char *output, uint8_t size) {
    while (size--) {
        *(output++) = NibbleToHex((*input) >> 4u);
        *(output++) = NibbleToHex((*input) & 0x0Fu);
        input++; /*< Move to the next byte. */
    }
}

uint8_t ReferenceNumber[8] = {0x30, 0x40, 0x60, 0x50, 0x80, 0x60, 0x75, 0x95};
HexToAscii(ReferenceNumber, output, sizeof(ReferenceNumber) / sizeof(ReferenceNumber[0])); 

Note: output must always the double the size of the input data (assuming that the size variable equals the length of the input data).

🌐
C For Dummies
c-for-dummies.com › blog
From Hex to Text | C For Dummies Blog
December 24, 2016 - To translate the rest of the codes into characters, I’d either have to examine an ASCII table or write code that translates the ASCII into text. I did the latter. Here you find the code I wrote to translate hex bytes, similar to those formatted above, and generate the corresponding ASCII string.
Find elsewhere
🌐
Blogger
embeddedlifehelp.blogspot.com › 2015 › 03 › c-code-to-convert-hex-into-ascii.html
Embedded Life: C code to convert Integer into ASCII HEX
March 28, 2015 - A blog about electronics design and embedded systems with detailed explanations of basic c and hardware implementation with code examples.
🌐
C For Dummies
c-for-dummies.com › blog
From Text to Hex | C For Dummies Blog
December 31, 2016 - In last week’s Lesson, I showed a program that translates a string of hex values into ASCII text. This code was to sate the nerd in me so that when another nerd writes 48 65 6c 6c 6f 2c 20 66 65 6c 6c 6f 77 20 6e 65 72 64 21, I can respond accordingly. But to respond in hex, a second program is required, one that translates ASCII text into a string of hex values.
🌐
Experts Exchange
experts-exchange.com › questions › 26661242 › Convert-HEX-string-to-ASCII-string-C-C.html
Solved: Convert HEX string to ASCII string C/C++ | Experts Exchange
December 6, 2010 - Going off the top of my head, you would do something like this to write your own function: int HexToInt( char* Hex ) { int nLen = strlen( Hex ); int nDigitMult = 1; int nResult = 0; for( int i = nLen - 1; i >= 0; i-- ) { char ch = Hex[ i ]; if( '0' <= ch && ch <= '9' ) nResult += (ch - '0') * nDigitMult; else if( 'a' <= ch && ch <= 'f' ) nResult += (ch - 'a' + 10) * nDigitMult; else if( 'A' <= ch && ch <= 'F' ) nResult += (ch - 'A' + 10) * nDigitMult; nDigitMult *= 16; } return nResult; } Then just use the classic itoa() to convert the number to a string. ... HooKooDooKu is returning one integer, not a string. Close, but not what was asked (plus int only gives you 4 bytes so you'll overflow in a hurry). Each two hex characters is one ascii character so it would look something like this.
🌐
RapidTables
rapidtables.com › convert › number › hex-to-ascii.html
Hex to String | Hex to ASCII Converter
Enter hex code bytes with any prefix / postfix / delimiter and press the Convert button (e.g. 45 78 61 6d 70 6C 65 21): * ASCII text encoding uses fixed 1 byte for each character.
🌐
Arm Community
community.arm.com › support-forums › f › keil-forum › 42152 › c-routine-for-hex-to-ascii
C Routine for Hex to ASCII - Keil forum - Support forums - Arm Community
September 11, 2003 - Hello Guys, I am doing a project. I am reading the output from the ADC,Hex value which i want to convert into ASCII value because i have written the code for
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-hexadecimal-value-string-ascii-value-string
Convert Hexadecimal value String to ASCII value String - GeeksforGeeks
July 11, 2025 - Once we have the bytes, we can use bitwise operations to convert them to characters in the ASCII string. In this implementation, we use a stringstream to build the ASCII string. We iterate through the hexadecimal string, converting each pair ...
🌐
Gary Kessler
garykessler.net › library › ascii.html
ascii decimal and hexadecimal conversion table
ASCII DECIMAL AND HEXADECIMAL CONVERSION TABLE · Extended ASCII Characters
🌐
CircuitDigest
circuitdigest.com › calculators › hex-to-ascii-calculator
HEX to ASCII Converter
April 28, 2022 - In the ASCII code, each of these characters are assigned a decimal number from 0 to 127. For example, the ASCII representation of upper case C is 67 and the lower case c is 99. ... To understand the how the calculator works let us look at an example of Hex to ASCII conversion.
🌐
Tomeko
tomeko.net › online_tools › ascii.php
ASCII to C-like array converter
0x and comma separator for output · See also: bin2hex.exe for Windows and bin2hex for Linux (source code)
🌐
Quora
quora.com › How-do-I-convert-hex-to-ASCII
How to convert hex to ASCII - Quora
Answer (1 of 4): This question does not have enough information to give a complete answer. It doesn’t tell me what language you programming in nor does it tell me what the character encoding is. I am going to answer assuming you are in C or C++ and that you have an integer or unsigned integer ...
🌐
Medium
medium.com › @convert.app › hex-to-ascii-converter-in-c-38ace601f94c
Hex to ASCII Converter in C++ Coding | by Convert Platform | Medium
February 23, 2025 - It extracts each two-character substring (hex pair) and converts it to an ASCII character. The main function takes user input, calls hexToAscii, and prints the result.