1. Overview

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.

2. Using the printf Command

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.

3. Using the echo Command

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.

4. Using the dc Command

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:

  • i represents the input radix. The value 16i is used for the hex radix.
  • P represents the Print command of dc.

5. Using the xxd Command

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:

  • -r option performs a reverse operation and converts the hex value to binary
  • -p option prints the result in a plain-text format

6. Conclusion

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.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.