Read an Ascii File in C and Convert to Binary

Binary to text (ASCII)

Hither you can convert binary to text. The translation will convert the binary lawmaking to decimal and then utilize the ASCII tabular array to correspond the characters behind those decimal codes.
Below y'all can learn how to do the conversion yourself and even how to implement such converter using the C programming language.

Convert binary to text


To generate binary lawmaking from ASCII text utilize the text to binary converter.



Auto format the input


How the conversion works?

Computers represent the information, using numbers. Every symbol has a distinct numeric code. The machinery of symbol representation is called character encoding.
I popular encoding is ASCII. We volition 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, nosotros need to do several tasks:

  1. Validate the input
  2. Format the input text if necessary. This helps the further actions.
  3. Take the binary digits(bits) in groups of 8. Nosotros phone call these groups "octets".
  4. Convert each octet to its decimal lawmaking.
  5. Interpret each decimal lawmaking to its symbol from the ASCII table.

Here is an example:

    010000010100001001000011

  1. The input contains just 0s and 1s. This is a valid binary lawmaking, so the validation is OK.
  2. For this example, it will be easier if nosotros split each octet with a space:
    01000001 01000010 01000011
  3. In this instance nosotros did this pace in 2.
  4. Convert the octets to their decimal:
    01000001 = 65
    01000010 = 66
    01000011 = 67
  5. 65 = A, 66 = B, 67 = C

    010000010100001001000011 = ABC

Binary alphabet

Using the binary alphabet nosotros can make the in a higher place conversion easier. Permit's say, that for some reason you convert binary to text on paper. We can skip step iv and directly map the binary value to its ASCII symbol. For this, we need a table to look up the mappings. Nosotros can call this table our binary alphabet.
In this mapping nosotros volition add the English letters (both majuscule and lowercase) and several other symbols that could be useful – space, comma, dot...

Capital letter Messages
A 01000001 H 01001000 O 01001111 V 01010110
B 01000010 I 01001001 P 01010000 Westward 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 North 01001110 U 01010101

Lowercase Letters
a 01100001 h 01101000 o 01101111 v 01110110
b 01100010 i 01101001 p 01110000 westward 01110111
c 01100011 j 01101010 q 01110001 x 01111000
d 01100100 grand 01101011 r 01110010 y 01111001
eastward 01100101 50 01101100 s 01110011 z 01111010
f 01100110 thou 01101101 t 01110100
g 01100111 n 01101110 u 01110101

Other symbols
(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.

Write your own binary to text converter

In this implementation nosotros will use the function from the binary to decimal conversion lesson.
The example implementation (free download: binary-to-text.goose egg) also has validation and formatting. Nosotros will non become into details in that location. Instead we will keep focus on the conversion itself.
The code is also available on Git Hub.

                    1  2  iii  4  five  6  7  8  9 x xi 12 13 14 xv 16 17 18
                    int                    master(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 effect text is: %s                    \n                    ", text);          free(text);                    return                    0; }                  

We aim to convert up to well-nigh 500 binary digits to ASCII. 496 is the closest multiple of 8, so that is the biggest binary size that nosotros will handle. This is enough to encode 62 ASCII symbols. The input could contain one space between the octets so we allocate 496 + 62 + one(null terminator) = 559 bytes.

Past default, scanf volition browse for a string until the first whitespace. This means that if we enter the 01000001 01000010, it will read only the start octet. To avoid this, we use the special formatter [^\n]. This tells scanf to read until information technology reads the new line symbol (the return key).

Then nosotros allocate the necessary memory for the result text and offset the conversion:

                    i  ii  3  4  5  6  7  8  9 ten xi
                    void                    binaryToText(char                    *binary,                    int                    binaryLength,                    char                    *text,                    int                    symbolCount) {                    int                    i;                    for(i                    =                    0; i                    <                    binaryLength; i+=                    eight, binary                    +=                    8)     {                    char                    *byte                    =                    binary;         byte[8]                    =                    '\0';                    *text++                    =                    binaryToDecimal(byte,                    8);     }     text                    -=                    symbolCount; }                  

Once we take the input in the correct format, the conversion is very uncomplicated. We take each octet, convert it to its decimal representation and save the resulting lawmaking. As we said earlier, we use the binaryToDecimal function, that we created in another lesson.

1 affair to look for, is to count the number of symbols. We need that count, then we tin can reset the arrow to the offset symbol.
Now the binary to text conversion is finished. We tin can print the consequence and gratis any memory that we allocated on the heap with the malloc function in a higher place.

Testing our program, we need to enter a binary input that will interpret to visible characters, then nosotros can verify that it is working as expected.
I used the ABC code from above 010000010100001001000011:

You can also exercise the reverse conversion. To meet how it is done become to text to binary lesson.

roneybuttle.blogspot.com

Source: https://www.c-programming-simple-steps.com/binary-to-text.html

0 Response to "Read an Ascii File in C and Convert to Binary"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel