1. Introduction

Most computers store the Latin characters using a decimal code. That code is called American Standard Code for Information Interchange (ASCII). Printing decimals as ASCII characters is useful when interpreting human-readable strings.

In this tutorial, we’ll explore a few ways to convert decimal numbers to their ASCII equivalent.

2. ASCII Maps

ASCII code is 7-bit and maps numbers to their character representation.

To understand, let’s create a file with a few characters and then explore the mapping using command-line tools:

$ echo "aAbB" > t.txt
$ hexdump -C t.txt
00000000  61 41 62 42 0a                                    |aAbB.|
$ hexdump -b t.txt
0000000 141 101 142 102 012

Firstly, we created a text file that has four characters aAbB. Next, we used the hexdump command with its -C option. This shows a hexadecimal representation of the characters in the sequence. We can see 61 is a representation of a, 41 is a representation of A, and so on.

The -b option of hexdump provides the octal representation of the characters in a data stream. Applied to our file, 141 is the octal representation of a, 101 is an octal representation of A, and so on. We can verify the mapping using the man page of ASCII.

To sum up, this gives us an understanding of how characters are mapped to various representations.

3. printf Using Octal

Given a decimal number, let’s convert it to its octal representation and print the octal number as a character using the printf Bash command:

$ for a in {64..70}
do
  oct=$(printf '%o' $a)<strong>;</strong>
  printf "$a --> \\$oct\n"
done
64 --> @
65 --> A
66 --> B
67 --> C
68 --> D
69 --> E
70 --> F

Using the sequence syntax, {64..70}, we iterate over the decimal numbers. Next, the printf command with a format specifier of %o outputs the octal representation of the number in $oct. The second printf command prints the decimal representation as well as the character using octal representation. Notably, the escape character \\ before the octal number instructs the printf command to output the character.

4. printf  Using Hexadecimal

Similarly, we can convert a decimal number to its hex representation and print the character using the printf Bash command:

$ for a in {64..70}
do
  hex=$(printf '%x' $a);
  printf "$a --> \\x$hex\n"
done
64 --> @
65 --> A
66 --> B
67 --> C
68 --> D
69 --> E
70 --> F

The printf command with the format specifier %x outputs the hex representation of a number in $hex. Next, the second printf command prints the decimal representation as well as the character using hex representation. Finally, the escape sequence \\x before the hex number instructs the printf command to print the character.

5. Using awk and echo

Finally, we can also use the standard echo and awk commands for our purposes:

$ for a in {64..70}
do
  echo $a | awk '{ printf "%d -- > %c\n", $1, $1 }';
done 
64 -- > @
65 -- > A
66 -- > B
67 -- > C
68 -- > D
69 -- > E
70 -- > F

As we see above, the echo command pipes the decimal number to awk. Next, the internal printf implementation of awk prints our character using the %c specifier, similar to the C Programming Language.

5. Conclusion

In this article, we talked about printing decimal values to ASCII.

First, we discussed the mapping of decimal to ASCII characters and some tools for converting the decimal representation to the corresponding character representation.

Secondly, we used a few variations of the format specifiers of the printf command to perform the conversion.

Finally, we also explored using the awk programming language in combination with the echo command to achieve the same result. In addition, we also learned to generate sequences of decimal numbers in the Bash shell.

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