ASCII text to binary

The text to binary converter takes text as input and produces binary code as output. Here you can try to convert some text. Later we will see how it works and you can try to do the example program in C.

Convert text to binary







Separate with spaces

To "decode" that back to text, use the binary to text converter.

The algorithm

We need to take the code of each character in decimal and convert it to binary. The input is basically a string, so taking each character from it is no problem. Reading the decimal code of a character is also a “one liner”. To do the conversion of that code to binary, we will use the function that we created in the decimal to binary lesson.

    If you want to do the text to binary by hand, you don't need to do the decimal to binary. You can use the direct mapping of a symbol to binary. To make it easier I prepared several tables with the binary alphabet.

We use up to 8 binary digits to represent the binary code. This limits the number of characters that we can represent to 256. Thus we will convert only ASCII text (unicode and other encodings that can represent thousands of symbols are not suitable for this example).

Create a converter in C

    The complete code of the example is available for free download from here: text-to-binary.zip

We start by reading up to 100 symbols from the standard input. We don't want to stop reading when the scanner “sees” a whitespace (including space). We explicitly instruct scanf to read until the first new line character, using the special format [^\n].

We want to format the output for easy reading, by inserting a space after each binary byte. For that reason we allocate 8 + 1=9 binary elements for each symbol from the input text.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
int main(void)
{
    char text[101];
    char *binary;
    int textLength, binaryLength;
    
    scanf("%100[^\n]s", text);
    textLength = strlen(text);
    binaryLength = textLength * 9;      // 8 binary digits + 1 space separator
    binary = malloc(binaryLength + 1);  // + 1 null terminator
    if(binary == NULL)
        exit(1);
    
    textToBinary(text, textLength, binary, binaryLength);
    printf("Your binary encoding is:\n%s\n", binary);
    
    free(binary);
    
    return 0;
}

What remains to be done is to:

  • read each character
  • take its decimal code
  • and convert it to binary, to complete the text to binary translation

Taking each character from a string is trivial – we just dereference the pointer:

    while(*text)

In C if we don't specify otherwise, the char is just a number. So extracting the decimal code is just as trivial. I explain the decimalToBinary function in the decimal to binary lesson.
So all you need is, to understand how the pointers work, the rest is easy as pie ;)

Several notes for the new guys:

  • *binary++
    This line does two things.
    1) dereference the pointer and use the value/memory, to which it points.
    2) After 1) is finished, increment the pointer, so it now points to the next character.
  • octet -= 8;
    Resets that pointer, so it references the first character in that octet. We need to do that, because after the calculations, it points to the last character, which is not useful for us.
  • malloc()
    Allocates the necessary memory on the heap for the pointer
  • free()
    Releases the memory allocated by malloc(). If you forget this, your program will leak memory.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
void textToBinary(char *text, int textLength, char *binary, int binaryLength)
{
    char *octet = malloc(9);
    if(octet == NULL)
        exit(1);
    while(*text)
    {
        decimalToBinary(*text, octet);
        while(*octet)
            *binary++ = *octet++;
        *binary++ = ' ';
        ++text;
        octet -= 8;
    }
    *binary = '\0';
    binary -= binaryLength;
    free(octet);
}

Now, you know how to convert ASCII text to binary. To do the reverse conversion, look here: binary-to-text.

   Search this site: