Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
One of the best things about Linux is that we can perform most operations using commands only. A good example of such operations is character conversions.
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. We can use this command to convert a character from hex to ASCII:
$ printf '\x4E'
N
The ASCII value of character N is 78, which is equivalent to 4E in hexadecimal. In this example, we’re simply using the \x format specifier to represent a hex number.
The echo command also supports various format specifiers. So, we can use it for character conversion:
$ echo -e "\x4E"
N
Note that we’ve used the -e option with the echo command. It enables interpretation of the escape sequences.
Alternatively, we can use the dc command to convert a character from hex to ASCII. The dc stands for Desk Calculator. This command accepts the input in postfix notation form:
$ echo "16i 4E P" | dc
N
In the above example:
The xxd command can generate a hex dump from a given input or reverse a hex dump given as input. We can use the reverse operation of this command for our character conversion:
$ echo 4E | xxd -r -p
N
In the above example:
In this article, we discussed various practical examples to convert a hex character to ASCII. First, we saw examples of the printf and echo commands. Then, we used a dc command. Finally, we saw the example of the xxd command. We can use these commands in day-to-day life to boost our productivity.