50140. File Encoder and Decoder

I'm a slow walker, but I never walk backwards.

Task Description

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.

exampleexample

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.

void bin2ascii(char input_file[32], char output_file[32], char dict[17]);
void ascii2bin(char input_file[32], char output_file[32], char dict[17]);

You can use the following $main.c$ to test your function.

#include<stdio.h>
#include"conversion.h"
int main(){
    int T;
    char input_file[32];
    char output_file[32];
    char dict[17];
    scanf("%d%s%s%s", &T, input_file, output_file, dict);
    if(T)ascii2bin(input_file, output_file, dict);
    else bin2ascii(input_file, output_file, dict);
    return 0;
}

Input

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.

Output

Write a file with given output filename.

Subtask

  • 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)

0
1-in.dat
1-out.dat
ABCDEFGHIJKLMNOP

Sample input 1 (binary file)

00000010

Sample output 1 (file)

AC

Sample Input 2 (stdin)

0
2-in.dat
2-out.dat
t5+%g83@&$r*-4~v

Sample input 2 (binary file)

10010010
01111010
11001000
11111010
00010000

Sample output 2 (file)

$+@r-&vr5t

Sample Input 3 (stdin)

1
3-in.dat
3-out.dat
ABCDEFGHIJKLMNOP

Sample input 3 ( file)

AC

Sample output 3 (binary file)

00000010

Sample Input 4 (stdin)

1
4-in.dat
4-out.dat
t5+%g83@&$r*-4~v

Sample input 4 (file)

$+@r-&vr5t

Sample output 4 (binary file)

10010010
01111010
11001000
11111010
00010000

Submit your conversion.c

Discussion