Here you can convert binary to text. The translation will convert the
binary code to decimal and then use the ASCII table to represent the
characters behind those decimal codes.
Below
you can learn how to do the conversion yourself and even how to
implement such converter using the C programming language.
To generate binary code from ASCII text use the text to binary converter.
Auto format the input |
|
|
Computers
represent the information, using numbers. Every symbol has a distinct
numeric code. The mechanism of symbol representation is called
character encoding.
One
popular encoding is ASCII. We will use the ASCII table to map the
binary octets to characters. Before we can do the translation, we need to convert the input from binary number system to decimal.
To do the conversion, we need to do several tasks:
Here is an example:
010000010100001001000011
010000010100001001000011 = ABC
Using the binary alphabet we can make the above conversion easier. Let's say, that for some reason you convert binary to text on paper. We can skip step 4 and directly map the binary value to its ASCII symbol. For this, we need a table to look up the mappings. We can call this table our binary alphabet.
In this mapping we will add the English letters (both capital and lowercase) and several other symbols that could be useful – space, comma, dot...
A | 01000001 | H | 01001000 | O | 01001111 | V | 01010110 |
B | 01000010 | I | 01001001 | P | 01010000 | W | 01010111 |
C | 01000011 | J | 01001010 | Q | 01010001 | X | 01011000 |
D | 01000100 | K | 01001011 | R | 01010010 | Y | 01011001 |
E | 01000101 | L | 01001100 | S | 01010011 | Z | 01011010 |
F | 01000110 | M | 01001101 | T | 01010100 | ||
G | 01000111 | N | 01001110 | U | 01010101 |
a | 01100001 | h | 01101000 | o | 01101111 | v | 01110110 |
b | 01100010 | i | 01101001 | p | 01110000 | w | 01110111 |
c | 01100011 | j | 01101010 | q | 01110001 | x | 01111000 |
d | 01100100 | k | 01101011 | r | 01110010 | y | 01111001 |
e | 01100101 | l | 01101100 | s | 01110011 | z | 01111010 |
f | 01100110 | m | 01101101 | t | 01110100 | ||
g | 01100111 | n | 01101110 | u | 01110101 |
(space) | 00100000 |
! | 00100001 |
" | 00100010 |
' | 00100111 |
, | 00101100 |
- | 00101101 |
. | 00101110 |
: | 00111010 |
; | 00111011 |
? | 00111111 |
For other symbols from the binary alphabet you can use the text to binary converter.
In this implementation we will use the function from the binary to decimal conversion lesson.
The example implementation (free download: binary-to-text.zip) also has validation and formatting. We will not go into details there. Instead we will keep focus on the conversion itself.
The code is also available on Git Hub.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | int main(void) { char binary[559]; char *text; int binaryLength, symbolCount; scanf("%558[^\n]s", binary); binaryLength = strlen(binary); symbolCount = binaryLength / 8 + 1; text = malloc(symbolCount + 1); binaryToText(binary, binaryLength, text, symbolCount); printf("The result text is: %s\n", text); free(text); return 0; } |
We aim to convert up to about 500 binary digits to ASCII. 496 is the closest multiple of 8, so that is the biggest binary size that we will handle. This is enough to encode 62 ASCII symbols. The input could contain one space between the octets so we allocate 496 + 62 + 1(null terminator) = 559 bytes.
By default, scanf will scan for a string until the first whitespace. This means that if we enter the 01000001 01000010, it will read only the first octet. To avoid this, we use the special formatter [^\n]. This tells scanf to read until it reads the new line symbol (the return key).
Then we allocate the necessary memory for the result text and start the conversion:
1 2 3 4 5 6 7 8 9 10 11 | void binaryToText(char *binary, int binaryLength, char *text, int symbolCount) { int i; for(i = 0; i < binaryLength; i+=8, binary += 8) { char *byte = binary; byte[8] = '\0'; *text++ = binaryToDecimal(byte, 8); } text -= symbolCount; } |
Once we have the input in the correct format, the conversion is very simple. We take each octet, convert it to its decimal representation and save the resulting code. As we said earlier, we use the binaryToDecimal function, that we created in another lesson.
One thing to look for, is to count the number of symbols. We need that count, so we can reset the pointer to the first symbol.
Now the binary to text conversion is finished. We can print the result and free any memory that we allocated on the heap with the malloc function above.
Testing our program, we need to enter a binary input that will translate to visible characters, so we can verify that it is working as expected.
I used the ABC code from above 010000010100001001000011:
You can also do the reverse conversion. To see how it is done go to text to binary lesson.