We have a binary file and a dictionary array of 16 printable characters. Now we want to encode the binary file into a printable string. First, we divide the binary file into bytes and each byte has $two\,chunks\,of\,four\,bits$. The most $significant\,four\,bits$ appear first in the sequence, and the least significant four bits then follow. We use each four bit chunk as index to retrieve a printable character from the dictionary array as $encoding$. We then store these encoding into a text file. The is called $encoding\,a\,binary\,file\,into\,an\,ASCII\,file$.
We also want to do the opposite, i.e., to decode an ASCII file back into binary. That is, we will have a ASCII file and a dictionary array of 16 printable characters, and we need to generate the original binary file. Note that all characters in the ASCII file must be present the array, and the number of them must be even.
Write two functions $bin2ascii$ and $ascii2bin$ for encoding and decoding respectively in $conversion.c$. You need to read from the input file and write the result to the output file. The input_file is the filename of input. So is the output_file.
The prototype of $conversion.h$ is as follows.
You can use the following $main.c$ to test your function.
, &T, input_file, output_file, dict);
(T)ascii2bin(input_file, output_file, dict);
bin2ascii(input_file, output_file, dict);
There are four lines in the input. The first line has the task type. Task type is 0 for encoding, and 1 for decoding. The second and third line contain the input and output filename respectively, and the last line consists of 16 printable characters as the dictionary array.
Write a file with given output filename.
- 10 points: do encoding and there is only one byte in the binary file.
- 40 points: do encoding and there are several bytes in the binary file.
- 10 points: do decoding and there are only two characters in the ASCII file.
- 40 points: do decoding and there are several characters in the ASCII file.
Sample Input 1 (stdin)
Sample input 1 (binary file)
Sample output 1 (file)
Sample Input 2 (stdin)
Sample input 2 (binary file)
Sample output 2 (file)
Sample Input 3 (stdin)
Sample input 3 ( file)
Sample output 3 (binary file)
Sample Input 4 (stdin)
Sample input 4 (file)
Sample output 4 (binary file)
Submit your conversion.c