If you want to go from the character code to the character itself, you include the character code in the printf format string, escaped with a backslash, in octal.

E.g. printf "\044\n" prints $ (and a newline).

In Bash and other shells, you could use hex, \x24, but that's not standard and doesn't work in Dash.

You could nest another printf in a command substitution to convert from hex or decimal to octal, though. Both of these would print $ (and a newline):

printf "\\$(printf %o 36)\n"
printf "\\$(printf %o 0x24)\n"
Answer from ilkkachu on Stack Exchange
🌐
Baeldung
baeldung.com › home › scripting › convert hex to ascii characters in the linux shell
Convert Hex to ASCII Characters in the Linux Shell | Baeldung on Linux
March 18, 2024 - In this quick tutorial, we’ll discuss some of the ways to convert a hex character to ASCII from the command line. The printf command formats and prints the data on the console.
Discussions

bash - Using `printf` to output chars given some ASCII numbers - Unix & Linux Stack Exchange
I've been trying to make printf output some chars, given their ASCII numbers (in hex)... something like this: #!/bin/bash hexchars () { printf '\x%s' $@ ;} hexchars 48 65 6c 6c 6f Expected output: More on unix.stackexchange.com
🌐 unix.stackexchange.com
March 21, 2021
shell - How do I print an ASCII character by different code points in Bash? - Unix & Linux Stack Exchange
while read c; do out+="${ascii:$c-1:1}"; done <<< "$in" echo "$out" ... printf "%o" "'J" # 112 (oct) printf "%d" "'J" # 74 (dec) printf "%x" "'J" # 4a (hex) printf '\112' # J (oct) printf "\x$(printf %x 74)" # J (dec, requires double conversion) printf '\x4a' # J (hex) ... According to ANSI-C ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
September 23, 2016
How to convert a Hex char buffer to an ASCII-string [C] - Stack Overflow
I need to convert an Hex string to an Ascii string. Not only display it with a printf but also to save it in memory. I use this code to display it but I would like to save it. How can I do?: #incl... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Stack Overflow
stackoverflow.com › questions › 8060170 › printing-hexadecimal-characters-in-c
hex - Printing hexadecimal characters in C - Stack Overflow
I'm trying to read in a line of characters, then print out the hexadecimal equivalent of the characters. For example, if I have a string that is "0xc0 0xc0 abc123", where the first 2 characters are c0 in hex and the remaining characters are abc123 in ASCII, then I should get · c0 c0 61 62 63 31 32 33 · However, printf using %x gives me ·
🌐
GNU
lists.gnu.org › archive › html › coreutils › 2014-12 › msg00030.html
Hex to ASCII conversion?
December 25, 2014 - $ printf "%c\n" h e l l o h e l l o Although of course I can use printf to convert from hex to decimal it looks like I can't use it for converting from hex to ascii. Of course the perl printf works like the C printf. $ perl -le 'printf("%c\n",0x48);' H So this works.
🌐
Linux Handbook
linuxhandbook.com › convert-hex-ascii
Convert Hex to ASCII Characters in Linux Command Line
July 27, 2025 - This method is a bit similar to the above but here you are using sed to filter and convert hex to ASCII using normal expression. echo -n 4c696e757868616e64626f6f6b20313233 | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf && echo ''
Top answer
1 of 7
25

Hex:

printf '\x4a'

Dec:

printf "\\$(printf %o 74)"

Alternative for hex :-)

xxd -r <<<'0 4a'
2 of 7
10

In general, the shell could understand hex, oct and decimal numbers in variables, provided they have been defined as integers:

$ declare -i v1 v2 v3 v4 v5 v6 v7
$ v1=0112
$ v2=74
$ v3=0x4a
$ v4=8#112
$ v5=10#74
$ v6=16#4a
$ v7=18#gg
echo "$v1 $v2 $v3 $v4 $v5 $v6 $v7"
74 74 74 74 74 74 304

Or they are the result of an "Arithmetic Expansion":

$ : $(( v1=0112, v2=74, v3=0x4a, v4=8#112, v5=10#74, v6=16#4a, v7=18#gg ))
$ echo "$v1 $v2 $v3 $v4 $v5 $v6 $v7"
74 74 74 74 74 74 304

So, you just need one way to print the character that belongs to a variable value.
But here are two possible ways:

$ var=$((0x65))
$ printf '%b\n' "\\$(printf '0%o' "$var")"
e

$ declare -i var
$ var=0x65; printf '%b\n' "\U$(printf '%08x' "$var")"
e

The two printf are needed, one to transform the value into an hexadecimal string and the second to actually print the character.

The second one will print any UNICODE point (if your console is correctly set).
For example:

$ var=0x2603; printf '%b\n' "\U$(printf '%08x' "$var")"
☃

An snow man.

The character that has an utf-8 representation as f0 9f 90 ae is 0x1F42E. Search for cow face site:fileformat.info to get it:

$ var=0x1F42F; printf '%b\n' "\U$(printf '%08x' "$var")"
🐮

Note: There is a problem with the UNICODE way in that for bash before 4.3 (corrected in that version and upwards), the characters between UNICODE points 128 and 255 (in decimal) may be incorrectly printed.


References

Fourth paragraph inside PARAMETERS in man bash:

If the variable has its integer attribute set, then value is evaluated as an arithmetic expression even if the $((...)) expansion is not used (see Arithmetic Expansion below).

Inside "ARITHMETIC EVALUATION" in man bash:

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the form [base#]n, where the optional base is a decimal number between 2 and 64 representing the arithmetic base, and n is a number in that base. If base# is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, @, and _, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.

Find elsewhere
Top answer
1 of 4
3

Okay, let's have a shot:

#include <ctype.h>
#include <stdio.h>

static unsigned char hexdigit2int(unsigned char xd)
{
  if (xd <= '9')
    return xd - '0';
  xd = tolower(xd);
  if (xd == 'a')
    return 10;
  if (xd == 'b')
    return 11;
  if (xd == 'c')
    return 12;
  if (xd == 'd')
    return 13;
  if (xd == 'e')
    return 14;
  if (xd == 'f')
    return 15;
  return 0;
}

int main(void)
{
  const char st[] = "48656C6C6F3B", *src = st;
  char text[sizeof st + 1], *dst = text;

  while (*src != '\0')
  {
    const unsigned char high = hexdigit2int(*src++);
    const unsigned char low  = hexdigit2int(*src++);
    *dst++ = (high << 4) | low;
  }
  *dst = '\0';
  printf("Converted '%s', got '%s'\n", st, text);
  return 0;
}

The very verbose-looking digit2int() function is trying to not be ASCII-dependent, which is always nice. Note that the loop in main() assumes proper input, it only checks for termination every two characters and won't handle non-hex data.

Oh, and this prints:

Converted '48656C6C6F3B', got 'Hello;'
2 of 4
0

as you probably know each character is represented by an int --> ...'a' = 97, 'b'=98... so once you convert an hex to int there is no need to create another function converting it to ascii - just by storing the integer into a character will do the trick (unless I'm missing something understanding the exercise).

as for storing it back to memory: there are many options: 1. remove the const before const char st[12] = "48656C6C6F3B"; and assign the return value from the function to the desired cell or using sscanf

take a look here

🌐
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
char hex[559]; · printf("Enter hex code to be converted to text: "); scanf("%558[^\n]s", hex); if(validate(hex)) { char* text = hexToAscii(hex); printf("The hex encoded text is:\n %s", text); free(text); } else · { printf("The ...
Author: ProgrammingSimpleSteps
🌐
CCS, Inc.
ccsinfo.com › forum › viewtopic.php
CCS :: View topic - RS232 printf - how do I send a hex value instead of ASCII
Profile Log in to check your private messages Log in · CCS does not monitor this forum on a regular basis
🌐
UbuntuMint
ubuntumint.com › home › linux commandline tips › how to convert hex to ascii characters in linux
How to Convert Hex to ASCII Characters in Linux
July 24, 2023 - $ printf '\x54\x65\x73\x74\x69... 1 2 3 · The sed command is used to filter and transform the Hex characters input by passing it via a regular expression for conversion to ASCII character before the final output is printed...
🌐
TutorialsPoint
tutorialspoint.com › article › convert-hex-to-ascii-characters-in-the-linux-shell
Convert Hex to ASCII Characters in the Linux Shell
March 17, 2026 - Use \x prefix to indicate hexadecimal values: $ printf '\x54\x75\x74\x6F\x72\x69\x61\x6C\x73\x20\x50\x6F\x69\x6E\x74' && echo ''
🌐
Unix.com
unix.com › shell programming and scripting
printf Hexadecimal output - Shell Programming and Scripting - Unix Linux Community
November 21, 2019 - printf "%X\n" "A" 41 printf "%X\n" "2" 2 Expected 32 (not 2). Is there a "printf" which will output the hexadecimal value of a numeric character?
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-hexadecimal-value-string-ascii-value-string
Convert Hexadecimal value String to ASCII value String - GeeksforGeeks
July 11, 2025 - ASCII is a standard that assigns letters, numbers, and other characters within the 256 slots available in the 8-bit code. E.g the lower case "h" character (Char) has a decimal value of 104, which is "01101000" in binary and “68” in hexadecimal.
🌐
IncludeHelp
includehelp.com › c › convert-ascii-string-to-hexadecimal-string-in-c.aspx
Convert ASCII string (char[]) to hexadecimal string (char[]) in C
Working with Hexadecimal values in C programming language · Working with octal values in C programming language · Convert ASCII string (char[]) to BYTE array in C · Convert ASCII string (char[]) to octal string (char[]) in C · How to assign binary value in a variable directly? How to check a particular bit is SET or not using C program? How to set, clear and toggle a single bit in C language? Value of 'EOF' in c programming language · How to print printf("Hello world."); using printf() in c programming ·
🌐
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.