You need to build a number in base10, considering you have all the digits stored in char*buf like so:

//assumtions: n - number of digits
//            digits are ordered MSD first -> buff[0] contains the most significant digit.
 for(i=0;i<n;i++)
    {
       number += buf[n-i-1]*pow(10,i); //number+=digit*10^i
    }

    printf("%x\n", number);
Answer from Pandrei 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
Discussions

c - Hex to ascii string conversion - Stack Overflow
i have an hex string and want it to be converted to ascii string in C. How can i accomplish this?? More on stackoverflow.com
🌐 stackoverflow.com
Convert the Ascii to Hex in C | Go4Expert
I am trying to convert the input ascii string to hex format using the format specifier "%.2X" . I have pasted the program at the end which does the... More on go4expert.com
🌐 go4expert.com
hex - Convert ascii char[] to hexadecimal char[] in C - Stack Overflow
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 c... More on stackoverflow.com
🌐 stackoverflow.com
Simple Ascii to Hex Conversion -
If I have an Ascii String in C. I want to examine each char of that String and check its hex Value. I want to then check for the occurrence of a particular hex value (example 0xffffff95) in that String. If I find it Just print a message to say it was encountered. More on experts-exchange.com
🌐 experts-exchange.com
March 7, 2005
🌐
IncludeHelp
includehelp.com › c › convert-ascii-string-to-hexadecimal-string-in-c.aspx
Convert ASCII string (char[]) to hexadecimal string (char[]) in C
Here, we created a function void string2hexString(char* input, char* output), to convert ASCII string to hex string, the final output string is storing in hex_str variable.
🌐
RapidTables
rapidtables.com › convert › number › ascii-to-hex.html
String to Hex | ASCII to Hex Code Converter
Get ASCII code of character from ASCII table · Convert decimal to hex byte · Continue with next character · Paste text in input text box. Select character encoding type. Select output delimiter string. Press the Convert button. Get letter · Get ASCII code of the letter from ASCII table ·
Find elsewhere
🌐
Go4Expert
go4expert.com › forums › convert-ascii-hex-c-t4967
Convert the Ascii to Hex in C | Go4Expert
I am trying to convert the input ascii string to hex format using the format specifier "%.2X" . I have pasted the program at the end which does the...
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 77086-no-atoh-function-c-ascii-hex-well-lets-create-one.html
No atoh() function in C ( Ascii To Hex )? - Well, Let's Create One
March 18, 2006 - The converted hex text decimal value is now 4278225015 And so this works fine. The conversion is made from the ascii hex input to actual hex values as confirmed by the decimal output. However, the problem here is that the user is limited to an input of only 4 HEX BYTES.
🌐
Gary Kessler
garykessler.net › library › ascii.html
ascii decimal and hexadecimal conversion table
ASCII DECIMAL AND HEXADECIMAL CONVERSION TABLE · Extended ASCII Characters
🌐
Experts Exchange
experts-exchange.com › questions › 21340758 › Simple-Ascii-to-Hex-Conversion.html
Solved: Simple Ascii to Hex Conversion - | Experts Exchange
March 7, 2005 - Thanks for you help :) ... >>>> How do I get Ascii 179 from the value 4294967219 >>>> and also 149 froom :[4294967189]. ASCII 149 == 0x95 ASCII 179 == 0xB3 You have to divide your hex number to pairs of two hex digits: 0xFFFFFFB3 == 0xFF 0xFF ...
🌐
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. ** UTF-8 text encoding uses a variable number of bytes for each character. This requires a delimiter between each ...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-a-string-to-hexadecimal-ascii-values
Convert a string to hexadecimal ASCII values - GeeksforGeeks
Consider every character from input, cast it into integer. This integer value is ascii value of that character. Change this integer value into hexadecimal value and add this hexadecimal value to final Hex string.
Published: July 11, 2025
🌐
C For Dummies
c-for-dummies.com › blog
From Hex to Text | C For Dummies Blog
December 24, 2016 - Because I found this utility useful, I saved in my personal bin folder. I named it hex2ascii. Naturally, being a nerd, I felt the need to write a companion program ascii2hex, which works the other way: It translates a short text string into a series of hex bytes.
🌐
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.
🌐
Reddit
reddit.com › r/embedded › coverting ascii char '5e' to hex value "0x5e"
r/embedded on Reddit: Coverting ASCII char '5E' to hex value "0x5E"
April 5, 2024 -

I am performing a checksum check on GPS packets.
Take the following GPS packets as an example,

"$GPGGA,002153.000,3342.6618,N,11751.3858,W,1,10,1.2,27.0,M,-34.2,M,,0000*5E\r\n"

Now to make sure the packet is valid; I need to compute the checksum of the packet and compare it with the checksum value already present in the incoming packet.

The problem I am facing is that the computed checksum (0x5E) has the type uint8_t and the checksum already present in the packet has the type of char. '5' has a char type and 'E' has a char.

If I typecast the char '5' with uint8_t I get '0x35'.

What is the simplest solution to this problem?

Top answer
1 of 4
42
You can just write a function that converts a hex character to an integer. The basic idea is that you can add and subtract chars since they're stored as u8s as you know. char c = '5'; uint8_t five = '5' - '0'; The function becomes something like below: uint8_t char_to_num(char c) { if (c >= '0' && c <= '9') { return c - '0'; } else if (c >= 'a' && c <= 'f') { return c - 'a' + 10; } else if (c >= 'A' && c <= 'F') { return c - 'A' + 10; } return -1; } Then if you want 0x5E, you can use bitwise OR the result of 5 << 4 and E to get 0x5E. uint8_t n1 = char_to_num('5'); uint8_t n2 = char_to_num('E'); uint8_t chk_sum = n1 << 4 | n2; But if you're parsing NMEA sentences from a GNSS receiver, you can just use the minmea library to do it for you. It's much easier than handling all the sentences yourself. Unless this is homework ofc.
2 of 4
17
you use strtoi()/strtou() with a base of 16 - or write your own function doing the same if you can't use these functions (e.g. because they are not defined or are too large/overkill for your environment). Or you could use sscanf(), look at the "%x" conversion. The ASCII range is not ordered in linear fashion w/ regard to the numbers in hexadecimal representation, so you can't get away with a simple cast and an addition, it's a little bit more complex, but just a little. Edge cases (lower/upper case letters) and error handling can make this a bit more difficult. Also, doing it the other way around and making a string comparison is also an option.
🌐
Utilities and Tools
utilities-online.info › ascii-to-hex
ASCII to Hex converter
The ASCII to Hex converter enables you to quickly convert the ASCII text into hexadecimal values.
🌐
CircuitDigest
circuitdigest.com › calculators › hex-to-ascii-calculator
HEX to ASCII Converter
April 28, 2022 - 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.
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).