public override CodedValueCollection GetCodes(string value) { var v = new StringBuilder(); foreach (char item in value.ToCharArray()) { v.Append(AsciiEncoder.Lookup(item)); } return(base.GetCodes(v.ToString())); }
public void TestSafe64EncodingAllChars() { //char count must by multiple of 4 for the compare to work string encoded = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; byte[] data = Safe64Encoding.DecodeBytes(encoded); Assert.AreEqual(encoded, Safe64Encoding.EncodeBytes(data)); data = AsciiEncoder.DecodeBytes(encoded); Assert.AreEqual(encoded, AsciiEncoder.EncodeBytes(data)); data = AsciiEncoder.DecodeBytes(Encoding.ASCII.GetBytes(encoded)); Assert.AreEqual(encoded, AsciiEncoder.EncodeBytes(data)); }
public void TestSafe64EncodingLargeArray() { Random rand = new Random(); byte[] data = new byte[0x400000]; rand.NextBytes(data); string testAsc = Safe64Encoding.EncodeBytes(data); Assert.AreEqual(0, BinaryComparer.Compare(data, Safe64Encoding.DecodeBytes(testAsc))); testAsc = AsciiEncoder.EncodeBytes(data); Assert.AreEqual(0, BinaryComparer.Compare(data, AsciiEncoder.DecodeBytes(testAsc))); }
void TestEncoderAgainstBase64(int repeat, int size) { Random rand = new Random(); byte[] data = new byte[size]; while (repeat-- > 0) { rand.NextBytes(data); string testB64 = Convert.ToBase64String(data); string testAsc = Safe64Encoding.EncodeBytes(data); Assert.AreEqual(testB64.Replace('+', '-').Replace('/', '_').Replace("=", ""), testAsc); Assert.AreEqual(0, BinaryComparer.Compare(data, Safe64Encoding.DecodeBytes(testAsc))); Assert.AreEqual(0, BinaryComparer.Compare(data, AsciiEncoder.DecodeBytes(testAsc))); } }
public override CodedValueCollection GetCodes(string value) { var codes = new CodedValueCollection(); foreach (char item in value.ToCharArray()) { if (item == ' ') { codes.Add(38); } else if (item == '$') { codes.Add(39); } else if (item == '/') { codes.Add(40); } else if (item == '+') { codes.Add(41); } else if (item == '%') { codes.Add(42); } else if (item >= '0' && item <= '9') { codes.Add(item - 48); } else { var tmp = AsciiEncoder.Lookup(item); switch (tmp[0]) { case '$': codes.Add(SHIFT1); codes.Add(tmp[1] - 55); break; case '%': codes.Add(SHIFT2); codes.Add(tmp[1] - 55); break; case '/': codes.Add(SHIFT3); codes.Add(tmp[1] - 55); break; case '+': codes.Add(SHIFT4); codes.Add(tmp[1] - 55); break; default: codes.Add(tmp[0] - 55); break; } } } codes.Insert(0, LIMIT); codes.Add(LIMIT); codes.Add(TERMINATOR); return(codes); }