Writing 5 bits at the time to a file
Posted on 2003-11-14
I have assignment for some wierd compression alghoritam that will read
in from a file convert characters to 5 bit codes and then write out
version of the original file. For example if input file contains string
and the codes are A=0, M=12,I=8,R=17 i am supposed to write out 3 byte file
The problem? How do I figure out shifting because I can only write out bytes
bits...I figure out I would "create" a byte and write it but the codes will
In my example I would have something like this in binary
00000001 00010001 00010000
where first 5 bits are hex 0, second 5 bits are 12 and so on...
Sometimes one created byte is based on codes of 3 different input
I have a loop in which I read a char at the time from input file, create a
int code and now I have to figure out
an alghoritam that would create a byte ( a char) that I can write out to
// get char at the time from input
while ((curr_chr = getc(in_file)) != EOF)
// get code for it
code = getCode(curr_chr);
// shifting code ......
// write created byte...
fwrite(&wbyte, 1, 1, out_file);
Any help is much appreciated....