Sunday, March 8, 2009

Tip #2: Displaying ASCII Codes

While a developer is working — particularly on a program that includes some sort of text-based interface — it is common to need to know the ASCII codes of one or more characters. Fortunately, most distributions ship with the showkey command:

linux@devbox:~/tip2> showkey -a

Press any keys - Ctrl-D will terminate this program
A 65 0101 0x41
b 98 0142 0x62
C 67 0103 0x43
^D 4 0004 0x04


As you can see, when you type a character, its ASCII code is displayed in decimal, octal, and hexadecimal. When finished entering characters, press Ctrl+D to exit.

ASCII codes are displayed by the showkey command when the -a option is specified. There are also modes to display scancodes (-s) and keycodes (-k or no argument), but most users will likely find themselves working in ASCII mode most frequently.

No comments: