The reason is because hexdump by default prints out 16-bit integers, not bytes. If your system has them, hd (or hexdump -C) or xxd will provide less surprising outputs - if not, od -t x1 is a POSIX-standard way to get byte-by-byte hex output. You can use od -t x1c to show both the byte hex values and the corresponding letters.

If you have xxd (which ships with vim), you can use xxd -r to convert back from hex (from the same format xxd produces). If you just have plain hex (just the '4161', which is produced by xxd -p) you can use xxd -r -p to convert back.

Answer from Random832 on Stack Overflow
Discussions

bash ascii to hex - Stack Overflow
was wondering if anyone could help me with converting ascii to hex in bash. Example code: #!/bin/bash STR = "hello" #Convert to hex HEXVAL = $STR #(in hex here?) I want hexval to have the value: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Conversion hex string into ascii in bash command line - Stack Overflow
You can use xxd to easily convert pairs of hex digits into ASCII, as it conveniently ignores the 0x prefix and any garbage (non-hex digits) in the data. If you take advantage of bash's built-in redirection (with a "here string," you won't even need to use echo or printf. More on stackoverflow.com
๐ŸŒ stackoverflow.com
bash - How to convert hex to ASCII characters in the Linux shell? - Stack Overflow
Let's say that I have a string 5a. This is the hex representation of the ASCII letter Z. I need to find a Linux shell command which will take a hex string and output the ASCII characters that the hex More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert column from hex to ascii
I can modify the column using awk but I can't pass that value to another command to do the convertion. Either you can do whatever you want, or this is homework. If so, please say so. Next, is this a Bash-only assignment, or can you use other languages? This is easy to do, but it would be much easier if you would post the assignment's hexadecimal source so I could show the conversion of your example text. Meanwhile, here's a sample: #!/usr/bin/env bash ascii="" for c in 54 68 69 73 20 69 73 20 61 20 74 65 73 74 2e; do ascii+="\x$c" done echo -e $ascii Output: This is a test. More on reddit.com
๐ŸŒ r/bash
21
5
July 25, 2019
๐ŸŒ
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 - The printf command formats and prints the data on the console. We can use this command to convert a character from hex to ASCII:
๐ŸŒ
DEV Community
dev.to โ€บ carlyraejepsenstan โ€บ converting-hex-to-text-the-bash-way-1483
Converting Hex to Text - the Bash Way - DEV Community
March 18, 2022 - What are we going to do to find the hidden message? First, we know that hexadecimal bytes are always 2 characters - you can verify this by using man ascii in your terminal and checking out the tables it shows.
Find elsewhere
๐ŸŒ
Raspberry Pi Forums
forums.raspberrypi.com โ€บ board index โ€บ software โ€บ raspberry pi os
Bash script: double ascii stream to hex .. and back - Raspberry Pi Forums
You don't need to keep saving, setting and then restoring extglob in the function itself, but the variable expansion won't work unless it is on at the time it is used. ... #!/bin/bash # hexish.sh - ascii to hex and back again - scruss # modified to use bash's extra variable globbing in toasc() - Paeryn function tohex() { echo -e $(sed 's/\(..\)/\\x\1/g;' <<< "$1") |\ sed 's/\(..\)/0x\1 /g; s/ $//;' } function toasc() { local restore_extglob=$(shopt -p extglob) shopt -s extglob echo -n "${1//+([[:space:]]|0x}" | xxd -pu $restore_extglob } # some test code: either first arg of script or canned example example='30323441' if [ $# -ge 1 ] then example=$1 fi hex=$(tohex "$example") asc=$(toasc "$hex") echo 'tohex' $example ' => ' $hex echo 'toasc' '"'$hex'"' ' => ' $asc
๐ŸŒ
Commandlinefu
commandlinefu.com โ€บ commands โ€บ view โ€บ 6081 โ€บ convert-ascii-string-to-hex
Convert ascii string to hex Using echo, od, sed
July 14, 2010 - echo -n \"text\" | od -A n -t x1 |sed 's/ /\\x/g' - (Convert ascii string to hex If you're going to use od, here's how to suppress the labels at the beginning. Also, it doesn't output the \x, hence the sed command at the end. Remove it for space separated hex values instead).
๐ŸŒ
LinuxQuestions.org
linuxquestions.org โ€บ questions โ€บ programming-9 โ€บ [bash]-ascii-to-hex-and-hex-to-ascii-488357-print
LinuxQuestions.org - [bash] ASCII to HEX and hex to ascii
- - [bash] ASCII to HEX and hex to ascii (https://www.linuxquestions.org/questions/programming-9/[bash]-ascii-to-hex-and-hex-to-ascii-488357/)
๐ŸŒ
Linux Handbook
linuxhandbook.com โ€บ convert-hex-ascii
Convert Hex to ASCII Characters in Linux Bash Shell
July 27, 2025 - ASCII is a method that is used to convert normal language characters to binary so computers can understand instructions from our side. Through this guide, I will include various ways by which you can easily convert hex to ASCII.
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.

๐ŸŒ
Reddit
reddit.com โ€บ r/bash โ€บ hex to ascii conversion - noob question
r/bash on Reddit: Hex to ASCII conversion - noob question
December 12, 2024 -

Hi all, freshly joined noobie here :)

I am currently working as a jr embedded software engineer, and have been struggling with data collection at runtime of the application.
I'm using a debugger that keeps sending a variable's hex value to the host pc via usb, but since this value is interpreted as ASCII, I see invalid symbols on the terminal.

As naive as it may sound, my question is: is there a way with a script to "get in between" the debugger and the terminal on the host pc to convert these hex values in their ASCII counterpart, so they are displayable "correctly"? (like, if I send 0x0123 I'd like the terminal to show "291" instead of the symbols associated with 0x01 and 0x23).
Extra question: do you have any suggestion on material I can study on to get a solid knowledge of bash scripting in general, too?

Thank you for your time and your patience, I hope I didn't sound too stupid haha.