Example #1
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            string      input       = "abc abc";
            HuffmanTree huffmanTree = new HuffmanTree();

            // Build the Huffman tree
            huffmanTree.Build(input);

            // Encode
            BitArray encoded = huffmanTree.Encode(input);

            Console.Write("Encoded: ");
            foreach (bool bit in encoded)
            {
                Console.Write((bit ? 1 : 0) + "");
            }
            Console.WriteLine();

            // Decode
            string decoded = huffmanTree.Decode(encoded);

            Console.WriteLine("Decoded: " + decoded);

            Console.ReadLine();
        }
Example #2
0
        public EncodedForm(string sourse)
        {
            int         aggregation = 1;
            HuffmanTree encoder     = new HuffmanTree();

            encoder.Build(sourse, aggregation);
            code = encoder.EncodeBits(sourse);
        }
Example #3
0
        private void Закодировать_Click(object sender, EventArgs e)
        {
            Dictionary <char, int> FrequenciesIn = new Dictionary <char, int>();


            string Encoding = SourceText.Text;

            FrequenciesIn = huffmanTree.Build(Encoding);
            encoded       = huffmanTree.Encode(Encoding);
            string LenghtTree = huffmanTree.Frequencies.Count.ToString();

            EncodedText.Text += "ivt" + LenghtTree + "\n";

            printTree(huffmanTree.node());
            foreach (bool bit in encoded)
            {
                EncodedText.Text += (bit ? 1 : 0);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Opening a file...");

            var fileToEncodePath         = @"C:\Users\Marcin\Downloads\tekst-dlugi.txt";
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileToEncodePath);

            string text;
            var    fileStream = new FileStream(fileToEncodePath, FileMode.Open, FileAccess.Read);

            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                text = streamReader.ReadToEnd();
            }

            Console.WriteLine($"Length in bits before encoding: {new BitArray(text.Select(c => c == '1').ToArray()).Length * 8}");

            string input       = text;
            var    huffmanTree = new HuffmanTree();

            // Build the Huffman tree
            Console.WriteLine("Building the Huffman tree...");
            huffmanTree.Build(input);

            // Encode
            Console.WriteLine("Encoding...");
            BitArray encoded = huffmanTree.Encode(input);


            foreach (var pair in huffmanTree.Frequencies)
            {
                Console.WriteLine($"Char: {pair.Key}, Frequency: {pair.Value}, Encoding: {new string(huffmanTree.Encodings[pair.Key].Select(x => x ? '1' : '0').ToArray())}");
            }

            byte[] bytes = new byte[encoded.Length / 8 + (encoded.Length % 8 == 0 ? 0 : 1)];
            encoded.CopyTo(bytes, 0);
            Console.WriteLine($"Length in bits after encoding: {bytes.Length * 8}");


            File.WriteAllBytes($@"C:\Users\Marcin\Downloads\{fileNameWithoutExtension}.huff", bytes);

            Console.WriteLine();
            Console.WriteLine("DONE");
            Console.ReadLine();
        }