私はハフマン圧縮/解凍を実行するプログラムを実装しています(学習/楽しむために、既存のライブラリ/プログラムを使用したくありません)。
なんとか圧縮3を作成できたので、すべての文字とそれぞれの圧縮表現をビット単位で表したテーブルがあります。例えば:
a = 0010 b = 01101 c = 0011 d = 1101 e = 101
今の私の考えは、ビットをコンテナ(たとえば、charまたはint変数)に格納してから、それらをファイルに出力することです。
ビット演算を使用してビットをcharまたはintにパック/アンパックする方法を知っています。しかし、私が直面している問題は、圧縮バージョンのビット数が、使用可能なビット数と一致しないことです。
Suppose I want to compress the string "abc" using the table above. I would start by compressing 'a', so packing 0010 into a char variable. Next I would compress 'b', but that requires 5 bits, and I only have 4 bits left on my char variable. I could use another variable, but it would become a mess to keep track what variable is using how many bits.
Using int would give me 32 bits to work with, but the same problem would happen once I get close to the limit.