Ejemplo n.º 1
0
        static void encode(string input, string fileName)
        {
            using (var file = new FileStream(fileName, FileMode.Create))
            {
            }

            var coder = new Coder();
            // input = input.Replace("\r","");
            var encodeStr = new StringBuilder(coder.Encode(input + "\n\0\n", fileName));
            var builder   = new StringBuilder();

            foreach (var freq in coder.getFrequencies())
            {
                builder.Append(freq.Key);
                builder.Append(" ");
                builder.Append(freq.Value);
                builder.Append("\n");
            }

            builder.Append("\n\0\n");

            var writer = new BinStream(fileName);

            writer.WriteBits(new BitArray(Encoding.GetEncoding(1251).GetBytes(builder.ToString())), FileMode.Append);

            OutputBits(new BinStream(fileName), encodeStr.ToString(), fileName);
        }
Ejemplo n.º 2
0
        private static void OutputBits(BinStream stream, string toWrite, string fileName)
        {
            var bytes   = BigInteger.Parse(toWrite).ToByteArray();
            var bitsStr = string.Concat(bytes.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')).Reverse());

            var list = new List <bool>(bitsStr.Length);

            foreach (var c in bitsStr)
            {
                list.Add(c != '0');
            }

            var bits = new BitArray(list.ToArray());

            stream.WriteBits(bits, FileMode.Append);
        }