Example #1
0
        static void Main(string[] args)
        {
            BitVirtualSteam bvStream = new BitVirtualSteam();

            // 12 bit (0,4096)
            bvStream.Write(4095, 12);
            int v = bvStream.ReadInt(12);

            var    bytes = HuffmanTrie.Compress("ABRACADABRA!");
            string str   = HuffmanTrie.Expand(bytes);

            //HuffmanTrie.Compress("it was the best of times it was the worst of times !");
            byte[] lzwBytes = LZW.Compress("ABRACADABRABRABRA");
            string lzwText  = LZW.Expand(lzwBytes);

            Console.WriteLine("Hello World!");
        }
Example #2
0
        public static string Expand(byte[] bytes)
        {
            int             index    = 0;
            BitArray        bits     = new BitArray(bytes);
            BitVirtualSteam bvStream = new BitVirtualSteam(bytes);
            HuffmanNode     root     = ReadTrie(bvStream, ref index);
            BitArray        lenbs    = new BitArray(32);

            for (int i = 0; i < 32; i++)
            {
                lenbs[i] = bits[index++];
            }
            byte[] lenBytes = new byte[4];
            lenbs.CopyTo(lenBytes, 0);
            //int N = BitConverter.ToInt32(lenBytes);
            int N = bvStream.ReadInt();

            char[] chars = new char[N];
            for (int i = 0; i < N; i++)
            {
                HuffmanNode x = root;
                while (!x.isLeaf())
                {
                    //一直往前读取
                    if (bvStream.ReadBoolean())
                    {
                        x = x.Right;
                    }
                    else
                    {
                        x = x.Left;
                    }
                }
                chars[i] = x.Ch;
            }
            return(string.Join("", chars));
        }