I'm having problems figuring out how to encode/decode.
This is the code i've got for encoding:
and the test string i'm been using is: "hello"Code:f = fopen("out.txt", "w"); for (i = 0; i < strlen(data); i++) { if (bit_pos == 8) { fputc(ch, f); ch = 0; bit_pos = 0; } for (y = 0; ; y++) { if (tree[data[i]].huf[y] == '\0') break; ch <<= 1; ch |= (tree[data[i]].huf[y] - 48); bit_pos++; } }
char : freq
h 1
e 1
l 2
o 1
encoded then is:
h : 111
e : 110
l : 0
o : 10
which then encoded with the above code comes out as: 1111100000000010
when its decoded then it comes out as:
hellllllllo
because the decoder is picking up the extra 0's as 'l' (which i suppose is right) but is there anyway around that or a better way of encoding?
Thanks