private string Encode()
        {
            byte[]       bytes             = Encoding.UTF8.GetBytes(String);
            BinaryString binaryStr         = BinaryString.FromString(String, Encoding.UTF8);
            BinaryString invertedBinaryStr = binaryStr.InvertBinaries();
            BinaryString reversedBinaryStr = invertedBinaryStr.ReverseBinaries();

            StringBuilder buffer = new StringBuilder();
            int           index;

            foreach (char c in reversedBinaryStr.ToString())
            {
                if (c == '0')
                {
                    index = _random.Next(_charsForZero.Length);
                    buffer.Append(_charsForZero[index]);
                }
                else
                {
                    index = _random.Next(_charsForOne.Length);
                    buffer.Append(_charsForOne[index]);
                }

                if (_random.Next(100) > 100 - InMiddleCharsStrength)
                {
                    index = _random.Next(_charsInTheMiddle.Length);
                    buffer.Append(_charsInTheMiddle[index]);
                }
            }

            return(buffer.ToString());
        }
        private string Decode()
        {
            StringBuilder buffer = new StringBuilder();

            foreach (char c in String.Where(x => !_charsInTheMiddle.Contains(x)))
            {
                if (_charsForZero.Contains(c))
                {
                    buffer.Append('0');
                }
                else if (_charsForOne.Contains(c))
                {
                    buffer.Append('1');
                }
                else
                {
                    throw new ArgumentException(string.Format("Invalid character '{0}'", c));
                }
            }

            BinaryString binaryStr         = BinaryString.FromBinaryString(buffer.ToString());
            BinaryString reversedBinaryStr = binaryStr.ReverseBinaries();
            BinaryString invertedBinaryStr = reversedBinaryStr.InvertBinaries();

            return(invertedBinaryStr.OriginalString);
        }
Exemple #3
0
        public void BinaryStringTest()
        {
            BinaryString str  = BinaryString.FromString("hello world");
            BinaryString str2 = BinaryString.FromString("hello world");

            Assert.AreEqual(str, str2);
            Assert.AreEqual("0110100001100101011011000110110001101111001000000111011101101111011100100110110001100100", str.ToString());

            BinaryString str3 = BinaryString.FromString("a");

            Assert.AreEqual("01100001", str3.ToString());

            BinaryString invertedStr3 = str3.InvertBinaries();

            Assert.AreEqual("10011110", invertedStr3.ToString());

            BinaryString reversedStr3 = str3.ReverseBinaries();

            Assert.AreEqual("10000110", reversedStr3.ToString());
        }