📅 May 20, 2018
Do you need a program that will convert an ASCII code into a character?
How about converting a number represented as ASCII into its binary equivalent based upon the data type you specify?
Try ascii2binary. Available from the Ubuntu repository, ascii2binary can be used alone or in scripts to convert a decimal, octal, binary, or hexadecimal input into its ASCII character or convert a text file of numbers into a binary file of numbers.
This was tested using Linux Mint 18.3.
sudo apt install ascii2binary
Also available using Synaptic.
By default, ascii2binary converts decimal values into ASCII characters. For the interactive mode, enter ascii2binary in a terminal and enter a code from 0 to 127 to see what happens. (It helps to have an ascii table open while doing this.)
As each decimal ASCII code was entered, its corresponding ASCII character was displayed on the next line. The next code must be entered on the same line as the character since Enter will exit the program.
For characters unable for display, such as 0 to 5 or 128 as shown, a unicode block or <?> symbol will appear.
We can also enter codes in hexadecimal.
ascii2binary -b h
We can even enter ASCII values as binary.
ascii2binary -b b
Another useful feature of ascii2binary is finding out the range of data types on a system.
This information may vary with the given platform, 32-bit or 64-bit, and processor. These are the results on a 64-bit CPU running Linux Mint 18.3 64-bit. If you ever need to look up this information, ascii2binary is a quick tool.
For more information, have a look at man ascii2binary.
“Wait a minute! We have been converting to ASCII characters by entering ASCII values, but the program is titled ascii2binary. Why not enter text and convert it into binary?”
Despite the name, ascii2binary does not operate as we might expect at first glance. Documentation about ascii2binary is sparse aside from the official web page and the man page. We cannot enter a text string, such as Hello! and expect ascii2binary to convert it into a series of binary digits.
“So, what does ascii2binary actually do?”
ascii2binary converts a textual representation of a number consisting of ASCII characters into its binary equivalent. For example, the decimal number 2,002 consist of ASCII number characters: 2, 0, 0, and 2. We can find 2 and 0 in an ASCII chart.
- The decimal number 9999 consists of four ASCII ‘9’ characters.
- 10,678 consists of five ASCII characters: 1, 0, 6, 7, 8, and a comma character.
- 9,999 consists of 9, 9, 9, 9, and a comma.
- 26 consists of the ASCII characters 2 and 6.
- …and so on for all numbers.
Notice that each number is represented by a sequence of ASCII number characters that represent that value. We can store the textual (ASCII) representation of a number in a text file or use it in a script or on the command line. In each case the number consists of ASCII number characters.
These numbers are ASCII characters, not binary. What if we want to convert a number that is spelled out using ASCII number characters into a binary value?
This is what ascii2binary is for. It will convert a number, such as 4,294,967,295, and convert it into its binary version, which is 0xFFFFFFFF (four bytes) as hexadecimal or 11111111 11111111 11111111 11111111 (32-bits) in binary.
Suppose we have a text file named number.txt that contains the decimal number 56. No other text is present since this will cause errors.
ascii2binary -t ui < number.txt > number.bin
The -t option specifies the output type. We are converting from an ASCII representation of a number into its binary equivalent. The type must be large enough to store the value. If you are not certain what your system can support, run ascii2binary -s as we did before to view what types are supported on your system. The ui says, “Store the binary value as an unsigned integer.” (man ascii2binary shows what abbreviations to use for the various output types.)
The number 56 will fit in every data type, signed or unsigned, since it fits within seven bits.
The command above generates a binary output, so we store that result in the binary file named number.bin. This is a binary file, so we cannot use a text editor to view it. We must open it in a hex editor to view its contents. Here is what number.bin contains when viewed in the GHex hex editor.
Example Unsigned Long
Let’s see what happens if we use the same number.txt file containing 56 (ASCII decimal) but change the binary output to an unsigned long with -t ul.
ascii2binary -t ul < number.txt > number.bin
Let’s try a number containing commas as a separator. The number.txt file has been changed to contain 9,098.
ascii2binary -t si < number.txt > number.bin
9,098 (decimal) is converted to 0x0000238A and stored as 0x8A230000 (little-endian, smallest byte first). Any commas in the number are ignored. This is another useful feature of ascii2binary: dealing with locale number separators.
We can store multiple numbers in a text file as ASCII and convert them into binary. All numbers must be of the same data type, so pick a size that will hold the largest number in the text file.
Here is number.txt with four numbers.
Since the largest number is 4,294,967,295, we must use at least an unsigned integer (four bytes stores 2^32 – 1). All numbers in the file (including smaller numbers) will be converted into an unsigned int data type.
ascii2binary -t ui < number.txt > number.bin
The highlighted byte marks the start of the second number, 888, represented as a sequence of four bytes. Remember, an Intel processor stores binary data as little-endian. Numbers are stored in binary in the same order that they appear in the text file.
- 99 -> 0x00000063 (hex) -> 0x63000000 (stored)
- 888 -> 0x00000378 (hex) -> 0x78030000 (stored)
- 9999 -> 0x0000270F (hex) -> 0x0F270000 (stored)
- 4,294,967,295 -> 0xFFFFFFFF (hex) -> 0xFFFFFFFF (stored)
“What good is this?”
Scripts and the command only deal with ASCII values that we can type from the keyboard. There are times when we need to evaluate raw binary values, but we must convert from ASCII into binary first. This is what ascii2binary is for.
More is possible with ascii2binary. For example, the ASCII numbers are not limited to decimal representations. We can input hex, octal, and binary as well, and those numbers will be converted into binary.
While not intuitive at first glance, ascii2binary is the kind of tool that you will wish existed and be glad it does exist for when you encounter a situation that requires it.
Explore and have fun!