public static void Main() { Char[] chars; Byte[] bytes = new Byte[] { 85, 84, 70, 55, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 }; UTF7Encoding utf7 = new UTF7Encoding(); int charCount = utf7.GetCharCount(bytes, 2, 8); chars = new Char[charCount]; int charsDecodedCount = utf7.GetChars(bytes, 2, 8, chars, 0); Console.WriteLine( "{0} characters used to decode bytes.", charsDecodedCount ); Console.Write("Decoded chars: "); foreach (Char c in chars) { Console.Write("[{0}]", c); } Console.WriteLine(); }
public void RFC1642_Example4() { string UniCodeString = "\u0049\u0074\u0065\u006D\u0020\u0033\u0020\u0069\u0073\u0020\u00A3\u0031\u002E"; char[] expected = UniCodeString.ToCharArray(); byte[] UTF7Bytes = new byte[] { 0x49, 0x74, 0x65, 0x6D, 0x20, 0x33, 0x20, 0x69, 0x73, 0x20, 0x2B, 0x41, 0x4B, 0x4D, 0x2D, 0x31, 0x2E }; UTF7Encoding UTF7enc = new UTF7Encoding(); char[] actual = UTF7enc.GetChars(UTF7Bytes); // "Item 3 is +AKM-1." is decoded as "Item 3 is <POUND SIGN>1." Assert.AreEqual(expected [0], actual [0], "UTF #1"); Assert.AreEqual(expected [1], actual [1], "UTF #2"); Assert.AreEqual(expected [2], actual [2], "UTF #3"); Assert.AreEqual(expected [3], actual [3], "UTF #4"); Assert.AreEqual(expected [4], actual [4], "UTF #5"); Assert.AreEqual(expected [5], actual [5], "UTF #6"); Assert.AreEqual(expected [6], actual [6], "UTF #7"); Assert.AreEqual(expected [7], actual [7], "UTF #8"); Assert.AreEqual(expected [8], actual [8], "UTF #9"); Assert.AreEqual(expected [9], actual [9], "UTF #10"); Assert.AreEqual(expected [10], actual [10], "UTF #11"); Assert.AreEqual(expected [11], actual [11], "UTF #12"); Assert.AreEqual(expected [12], actual [12], "UTF #13"); Assert.AreEqual(UniCodeString, UTF7enc.GetString(UTF7Bytes), "GetString"); }
public void RFC1642_Example2() { string UniCodeString = "\u0048\u0069\u0020\u004D\u006F\u004D\u0020\u263A\u0021"; char[] expected = UniCodeString.ToCharArray(); byte[] UTF7Bytes = new byte[] { 0x48, 0x69, 0x20, 0x4D, 0x6F, 0x4D, 0x20, 0x2B, 0x4A, 0x6A, 0x6F, 0x41, 0x49, 0x51, 0x2D }; UTF7Encoding UTF7enc = new UTF7Encoding(); char[] actual = UTF7enc.GetChars(UTF7Bytes); // "Hi Mom +Jjo-!" is decoded as "Hi Mom <WHITE SMILING FACE>!" Assert.AreEqual(expected [0], actual [0], "UTF #1"); Assert.AreEqual(expected [1], actual [1], "UTF #2"); Assert.AreEqual(expected [2], actual [2], "UTF #3"); Assert.AreEqual(expected [3], actual [3], "UTF #4"); Assert.AreEqual(expected [4], actual [4], "UTF #5"); Assert.AreEqual(expected [5], actual [5], "UTF #6"); Assert.AreEqual(expected [6], actual [6], "UTF #7"); Assert.AreEqual(expected [7], actual [7], "UTF #8"); Assert.AreEqual(expected [8], actual [8], "UTF #9"); Assert.AreEqual(UniCodeString, UTF7enc.GetString(UTF7Bytes), "GetString"); }
public static void Main() { // A few optional characters. string chars = "!@#$"; // The default Encoding does not allow optional characters. // Alternate byte values are used. UTF7Encoding utf7 = new UTF7Encoding(); Byte[] bytes1 = utf7.GetBytes(chars); Console.WriteLine("Default UTF7 Encoding:"); ShowArray(bytes1); // Convert back to characters. Console.WriteLine("Characters:"); ShowArray(utf7.GetChars(bytes1)); // Now, allow optional characters. // Optional characters are encoded with their normal code points. UTF7Encoding utf7AllowOptionals = new UTF7Encoding(true); Byte[] bytes2 = utf7AllowOptionals.GetBytes(chars); Console.WriteLine("UTF7 Encoding with optional characters allowed:"); ShowArray(bytes2); // Convert back to characters. Console.WriteLine("Characters:"); ShowArray(utf7AllowOptionals.GetChars(bytes2)); }
public void PosTest2() { Char[] chars; Byte[] bytes = new Byte[] { }; UTF7Encoding UTF7 = new UTF7Encoding(); int charCount = UTF7.GetCharCount(bytes, 0, 0); chars = new Char[] { }; int charsDecodedCount = UTF7.GetChars(bytes, 0, 0, chars, 0); Assert.Equal(0, charsDecodedCount); }
public void NegTest1() { Char[] chars; Byte[] bytes = null; UTF7Encoding UTF7 = new UTF7Encoding(); chars = new Char[] { }; Assert.Throws<ArgumentNullException>(() => { int charsDecodedCount = UTF7.GetChars(bytes, 0, 0, chars, 0); }); }
public void NegTest1() { Char[] chars; Byte[] bytes = null; UTF7Encoding UTF7 = new UTF7Encoding(); chars = new Char[] { }; Assert.Throws <ArgumentNullException>(() => { int charsDecodedCount = UTF7.GetChars(bytes, 0, 0, chars, 0); }); }
public void PosTest2() { Char[] chars; Byte[] bytes = new Byte[] { }; UTF7Encoding UTF7 = new UTF7Encoding(); int charCount = UTF7.GetCharCount(bytes, 0, 0); chars = new Char[] { }; int charsDecodedCount = UTF7.GetChars(bytes, 0, 0, chars, 0); Assert.Equal(0, charsDecodedCount); }
public void PosTest1() { Char[] chars; Byte[] bytes = new Byte[] { 85, 84, 70, 55, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 }; UTF7Encoding UTF7 = new UTF7Encoding(); int charCount = UTF7.GetCharCount(bytes, 2, 8); chars = new Char[charCount]; int charsDecodedCount = UTF7.GetChars(bytes, 2, 8, chars, 0); }
public void PosTest1() { Char[] chars; Byte[] bytes = new Byte[] { 85, 84, 70, 55, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 }; UTF7Encoding UTF7 = new UTF7Encoding(); int charCount = UTF7.GetCharCount(bytes, 2, 8); chars = new Char[charCount]; int charsDecodedCount = UTF7.GetChars(bytes, 2, 8, chars, 0); }
public void NegTest2() { Char[] chars = null; Byte[] bytes = new Byte[] { 85, 84, 70, 55, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 }; UTF7Encoding UTF7 = new UTF7Encoding(); Assert.Throws<ArgumentNullException>(() => { int charsDecodedCount = UTF7.GetChars(bytes, 2, 8, chars, 0); }); }
public void NegTest2() { Char[] chars = null; Byte[] bytes = new Byte[] { 85, 84, 70, 55, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 }; UTF7Encoding UTF7 = new UTF7Encoding(); Assert.Throws <ArgumentNullException>(() => { int charsDecodedCount = UTF7.GetChars(bytes, 2, 8, chars, 0); }); }
public void TestCompare256WithUtf7() { Encoding utf7 = new UTF7Encoding(allowOptionals: true); Encoding penc = new PythonSurrogateEscapeEncoding(utf7); // The following Python output is produced with python 3.4 but is not correct: it is missing the '+' character string python_chars = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\udc80\udc81\udc82\udc83\udc84\udc85\udc86\udc87\udc88\udc89\udc8a\udc8b\udc8c\udc8d\udc8e\udc8f\udc90\udc91\udc92\udc93\udc94\udc95\udc96\udc97\udc98\udc99\udc9a\udc9b\udc9c\udc9d\udc9e\udc9f\udca0\udca1\udca2\udca3\udca4\udca5\udca6\udca7\udca8\udca9\udcaa\udcab\udcac\udcad\udcae\udcaf\udcb0\udcb1\udcb2\udcb3\udcb4\udcb5\udcb6\udcb7\udcb8\udcb9\udcba\udcbb\udcbc\udcbd\udcbe\udcbf\udcc0\udcc1\udcc2\udcc3\udcc4\udcc5\udcc6\udcc7\udcc8\udcc9\udcca\udccb\udccc\udccd\udcce\udccf\udcd0\udcd1\udcd2\udcd3\udcd4\udcd5\udcd6\udcd7\udcd8\udcd9\udcda\udcdb\udcdc\udcdd\udcde\udcdf\udce0\udce1\udce2\udce3\udce4\udce5\udce6\udce7\udce8\udce9\udcea\udceb\udcec\udced\udcee\udcef\udcf0\udcf1\udcf2\udcf3\udcf4\udcf5\udcf6\udcf7\udcf8\udcf9\udcfa\udcfb\udcfc\udcfd\udcfe\udcff"; // Our implementation will refuse (correctly) to decode because the ',' after '+' is not valid thus requires escaping, // but escaping of chars under 128 is not allowed. Assert.Throws <DecoderFallbackException>(() => penc.GetChars(bytes)); // Let's try again without the '+' bytes = bytes.Where(i => i != (byte)'+').ToArray(); char[] chars = penc.GetChars(bytes); Assert.AreEqual(python_chars, chars); // Now the encoding part byte[] encoded_bytes = penc.GetBytes(chars); byte[] expected_bytes = "+AAAAAQACAAMABAAFAAYABwAI-\t\n+AAsADA-\r+AA4ADwAQABEAEgATABQAFQAWABcAGAAZABoAGwAcAB0AHgAf- !\"#$%&'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[+AFw-]^_`abcdefghijklmnopqrstuvwxyz{|}+AH4Af9yA3IHcgtyD3ITchdyG3IfciNyJ3Irci9yM3I3cjtyP3JDckdyS3JPclNyV3Jbcl9yY3Jncmtyb3Jzcndye3J/coNyh3KLco9yk3KXcptyn3Kjcqdyq3KvcrNyt3K7cr9yw3LHcstyz3LTctdy23LfcuNy53Lrcu9y83L3cvty/3MDcwdzC3MPcxNzF3Mbcx9zI3MncytzL3MzczdzO3M/c0NzR3NLc09zU3NXc1tzX3Njc2dza3Nvc3Nzd3N7c39zg3OHc4tzj3OTc5dzm3Ofc6Nzp3Orc69zs3O3c7tzv3PDc8dzy3PPc9Nz13Pbc99z43Pnc+tz73Pzc/dz+3P8-" .Select(c => (byte)c).ToArray(); Assert.AreEqual(expected_bytes, encoded_bytes); // Encoding the given chars with CPython produces the following byte string byte[] python_bytes = "+AAAAAQACAAMABAAFAAYABwAI\t\n+AAsADA\r+AA4ADwAQABEAEgATABQAFQAWABcAGAAZABoAGwAcAB0AHgAf !\"#$%&'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[+AFw]^_`abcdefghijklmnopqrstuvwxyz{|}+AH4Af9yA3IHcgtyD3ITchdyG3IfciNyJ3Irci9yM3I3cjtyP3JDckdyS3JPclNyV3Jbcl9yY3Jncmtyb3Jzcndye3J/coNyh3KLco9yk3KXcptyn3Kjcqdyq3KvcrNyt3K7cr9yw3LHcstyz3LTctdy23LfcuNy53Lrcu9y83L3cvty/3MDcwdzC3MPcxNzF3Mbcx9zI3MncytzL3MzczdzO3M/c0NzR3NLc09zU3NXc1tzX3Njc2dza3Nvc3Nzd3N7c39zg3OHc4tzj3OTc5dzm3Ofc6Nzp3Orc69zs3O3c7tzv3PDc8dzy3PPc9Nz13Pbc99z43Pnc+tz73Pzc/dz+3P8-" .Select(c => (byte)c).ToArray(); // The sequences expected_bytes and python_bytes are NOT equal: .NET ends encoded blocks (starting with '+') with '-' // Terminating encoded blocks with '-' is optional if not ambiguous. // CPython doesn't terminate blocks with '-' if not mandatory, resulting in a more compact encoding. // However, they both decode to the same text, although, again, CPython's version cannot be decoded using surrogateescape char[] dotnet_decoded = penc.GetChars(encoded_bytes); char[] python_decoded = utf7.GetChars(python_bytes); Assert.AreEqual(chars, python_decoded); Assert.AreEqual(chars, dotnet_decoded); dotnet_decoded = utf7.GetChars(encoded_bytes); Assert.AreEqual(chars, dotnet_decoded); }
public void NegTest3() { Char[] chars; Byte[] bytes = new Byte[] { 85, 84, 70, 55, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 }; UTF7Encoding UTF7 = new UTF7Encoding(); int charCount = UTF7.GetCharCount(bytes, 2, 8); chars = new Char[charCount]; Assert.Throws<ArgumentOutOfRangeException>(() => { int charsDecodedCount = UTF7.GetChars(bytes, -2, 8, chars, 0); }); }
public void NegTest4() { Char[] chars; Byte[] bytes = new Byte[] { 85, 84, 70, 55, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 }; UTF7Encoding UTF7 = new UTF7Encoding(); int charCount = UTF7.GetCharCount(bytes, 2, 8); chars = new Char[charCount]; Assert.Throws <ArgumentOutOfRangeException>(() => { int charsDecodedCount = UTF7.GetChars(bytes, 2, -8, chars, 0); }); }
public void RFC1642_Example1() { string UniCodeString = "\u0041\u2262\u0391\u002E"; char[] expected = UniCodeString.ToCharArray(); byte[] UTF7Bytes = new byte [] { 0x41, 0x2B, 0x49, 0x6D, 0x49, 0x44, 0x6B, 0x51, 0x2D, 0x2E }; UTF7Encoding UTF7enc = new UTF7Encoding(); char[] actual = UTF7enc.GetChars(UTF7Bytes); // "A+ImIDkQ-." is decoded as "A<NOT IDENTICAL TO><ALPHA>." see RFC 1642 Assert.AreEqual(expected [0], actual [0], "UTF #1"); Assert.AreEqual(expected [1], actual [1], "UTF #2"); Assert.AreEqual(expected [2], actual [2], "UTF #3"); Assert.AreEqual(expected [3], actual [3], "UTF #4"); Assert.AreEqual(UniCodeString, UTF7enc.GetString(UTF7Bytes), "GetString"); }
public void RFC1642_Example3() { string UniCodeString = "\u65E5\u672C\u8A9E"; char[] expected = UniCodeString.ToCharArray(); byte[] UTF7Bytes = new byte[] { 0x2B, 0x5A, 0x65, 0x56, 0x6E, 0x4C, 0x49, 0x71, 0x65, 0x2D }; UTF7Encoding UTF7enc = new UTF7Encoding(); char[] actual = UTF7enc.GetChars(UTF7Bytes); // "+ZeVnLIqe-" is decoded as Japanese "nihongo" Assert.AreEqual(expected [0], actual [0], "UTF #1"); Assert.AreEqual(expected [1], actual [1], "UTF #2"); Assert.AreEqual(expected [2], actual [2], "UTF #3"); Assert.AreEqual(UniCodeString, UTF7enc.GetString(UTF7Bytes), "GetString"); }
/// <summary> /// Gets the cell address. /// </summary> /// <param name="rowIndex">Index of the row.</param> /// <param name="columnIndex">Index of the column.</param> /// <returns>Cell address string.</returns> private String GetCellAddress(Int32 rowIndex, Int32 columnIndex) { Byte[] bytes; Int32 columnDiv, columnReminder; String cellAdress; columnDiv = (columnIndex - 1) / 26; columnReminder = ((columnIndex - 1) % 26) + 1; if (columnDiv > 0) { bytes = new Byte[2]; bytes[0] = (Byte)(64 + columnDiv); bytes[1] = (Byte)(64 + columnReminder); } else { bytes = new Byte[1]; bytes[0] = (Byte)(64 + columnReminder); } cellAdress = new String(_utf7Encoding.GetChars(bytes)) + rowIndex; return(cellAdress); }