📅 March 31, 2015
Here is the scenario: You are typing away furiously at the command line when you suddenly need the ASCII hex code for the caret character (^). What do you do?
Sure, you could interrupt your typing and consult a GUI character map using the mouse, but why disturb the finger flow when you could easily open a new terminal with a keyboard shortcut and run a simple program that prints an ASCII chart?
That program is called ascii.
Installing
ascii is freely available from the repository (Ubuntu/Linux Mint). It is not installed by default, so install it using Synaptic or the command line.
sudo apt-get update sudo apt-get install ascii
Using
Type ascii at the command prompt.
ascii
That’s it. An ASCII table of characters 0 through 127 will appear. A few options will adjust the output, and man ascii will provide the full details. Let’s see a few examples.
List only hex codes:
ascii -x
Show code for one character:
ascii A
ascii a
You can even use a character’s official name:
ascii miniscule b
ascii DEL
Show code for a single character but without details:
ascii -s a
Show codes for characters in a string:
ascii -s 'Linux is fun!'
Reverse conversions are also possible. Suppose you only know the decimal value of and ASCII character an wish to known what character it is?
ascii d52
To specify decimal, prefix the decimal value with a lowercase d or else the output will not be correct.
Reverse lookup using hex:
ascii 0x34
Prefix with 0x to denote hexadecimal.
How about a bit pattern lookup?
ascii 01010010
Even though the man page instructs a prefix, such as b or 0b, only the bit pattern by itself returned a result. Bit patterns beyond 127d will produce the meta characters not covered by the standard ASCII table.
For fun, feed fortune into ascii to view all character codes instead of the text:
ascii -s "$(fortune)"
The output will vary. Include double quotes to allow command substitution and to handle spaces.
How about the current date?
ascii -s "$(date)"
To print only the binary codes for each character (the fifth column):
ascii -s $(date) | awk '{print $5}'
How about this? Remove the newlines to produce a series of ones and zeros containing the current date:
ascii -s $(date) | awk '{print $5}' | tr -d '\n'
We can do the same for fortune:
ascii -s $(fortune) | awk '{print $5}' | tr -d '\n'
To test your sanity (or to make others vacate the room), pipe the result to espeak:
ascii -s $(fortune) | awk '{print $5}' | tr -d '\n' | espeak
Make sure to turn your speakers up loud for maximum annoyance! (It’s safe to assume that CTRL+C will be used quickly.)
With creativity, ascii can do more than show a simple ascii chart, so have fun!