# 50140. File Encoder and Decoder

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

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.

example

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.

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

01-in.dat1-out.datABCDEFGHIJKLMNOP


## Sample input 1 (binary file)

00000010


## Sample output 1 (file)

AC


02-in.dat2-out.datt5+%g83@&$r*-4~v  ## Sample input 2 (binary file) 1001001001111010110010001111101000010000  ## Sample output 2 (file) $+@r-&vr5t


## Sample Input 3 (stdin)

13-in.dat3-out.datABCDEFGHIJKLMNOP


## Sample input 3 ( file)

AC


## Sample output 3 (binary file)

00000010


14-in.dat4-out.datt5+%g83@&$r*-4~v  ## Sample input 4 (file) $+@r-&vr5t


## Sample output 4 (binary file)

1001001001111010110010001111101000010000