public void PosTest2() { UTF7Encoding utf71 = new UTF7Encoding(); UTF7Encoding utf72 = new UTF7Encoding(); utf71 = utf72; Assert.True(utf71.Equals(utf72)); }
public void PosTest2() { Byte[] bytes = new Byte[] { }; UTF7Encoding UTF7 = new UTF7Encoding(); int charCount = UTF7.GetCharCount(bytes, 0, 0); Assert.Equal(0, charCount); }
public void NegTest1() { Assert.Throws<ArgumentNullException>(() => { UTF7Encoding UTF7 = new UTF7Encoding(); UTF7.GetByteCount(null, 0, 1); }); }
public BfsBinaryReader(BinaryReader binaryReader, Endianness fileEndianness) { reader = binaryReader; FileEndianness = fileEndianness; asciienc = new ASCIIEncoding(); utf7enc = new UTF7Encoding(); utf32enc = new UTF32Encoding(); }
public static IEnumerable<object[]> GetChars_TestData() { 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 }; int charCount = new UTF7Encoding().GetCharCount(bytes, 2, 8); char[] chars = new char[charCount]; yield return new object[] { bytes, 2, 8, chars, 0, new char[] { 'F', '7', ' ', 'E', 'n', 'c', 'o', 'd' } }; yield return new object[] { new byte[0], 0, 0, new char[0], 0, new char[0] }; }
public void NegTest1() { Byte[] bytes = null; UTF7Encoding utf7 = new UTF7Encoding(); Assert.Throws<ArgumentNullException>(() => { string str = utf7.GetString(bytes, 0, 2); }); }
public void NegTest1() { UTF7Encoding utf7 = new UTF7Encoding(); int charCount = -1; Assert.Throws<ArgumentOutOfRangeException>(() => { int maxByteCount = utf7.GetMaxByteCount(charCount); }); }
public void PosTest1() { Byte[] bytes = new Byte[] { 85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101}; UTF7Encoding utf7 = new UTF7Encoding(); string str = utf7.GetString(bytes, 0, bytes.Length); }
public void NegTest1() { Byte[] bytes = null; UTF7Encoding UTF7 = new UTF7Encoding(); Assert.Throws<ArgumentNullException>(() => { int charCount = UTF7.GetCharCount(bytes, 2, 8); }); }
public void NegTest1() { string source = null; Assert.Throws<ArgumentNullException>(() => { UTF7Encoding UTF7 = new UTF7Encoding(); UTF7.GetByteCount(source); }); }
public void PosTest1() { Byte[] bytes; String chars = "UTF7 Encoding Example"; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = chars.Length; bytes = new Byte[byteCount]; int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }
public void PosTest2() { Byte[] bytes; String chars = ""; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = chars.Length; bytes = new Byte[byteCount]; int bytesEncodedCount = UTF7.GetBytes(chars, 0, byteCount, bytes, 0); Assert.Equal(0, bytesEncodedCount); }
public void PosTest1() { Byte[] bytes = new Byte[] { 85, 84, 70, 56, 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); Assert.Equal(8, charCount); }
public void PosTest2() { Byte[] bytes; Char[] chars = new Char[] { }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 0, 0); bytes = new Byte[byteCount]; int bytesEncodedCount = UTF7.GetBytes(chars, 0, 0, bytes, 0); Assert.Equal(0, bytesEncodedCount); }
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 NegTest2() { Byte[] bytes; String chars = "UTF7 Encoding Example"; UTF7Encoding UTF7 = new UTF7Encoding(); bytes = null; Assert.Throws<ArgumentNullException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }); }
public static IEnumerable<object[]> GetBytes_TestData() { string chars1 = "\u0023\u0025\u03a0\u03a3"; int charsLength = new UTF7Encoding().GetByteCount(chars1.ToCharArray(), 1, 2); yield return new object[] { chars1, 1, 2, new byte[charsLength], 0, new byte[] { 43, 65, 67, 85, 68, 111, 65, 45 } }; yield return new object[] { string.Empty, 0, 0, new byte[0], 0, new byte[0] }; string chars2 = "UTF7 Encoding Example"; yield return new object[] { chars2, 1, 2, new byte[chars2.Length], 0, new byte[] { 84, 70 } }; }
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 NegTest4() { Byte[] bytes = new Byte[] { 85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101}; UTF7Encoding UTF7 = new UTF7Encoding(); Assert.Throws<ArgumentOutOfRangeException>(() => { int charCount = UTF7.GetCharCount(bytes, bytes.Length, 6); }); }
public void NegTest4() { Byte[] bytes; String chars = "UTF7 Encoding Example"; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = chars.Length; bytes = new Byte[byteCount]; Assert.Throws<ArgumentOutOfRangeException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, -2, bytes, 0); }); }
public void NegTest3() { Byte[] bytes = new Byte[] { 85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101}; UTF7Encoding utf7 = new UTF7Encoding(); Assert.Throws<ArgumentOutOfRangeException>(() => { string str = utf7.GetString(bytes, 0, -1); }); }
public void NegTest1() { Byte[] bytes; String chars = null; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = 10; bytes = new Byte[byteCount]; Assert.Throws<ArgumentNullException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 0); }); }
public void PosTest2() { int startIndex = 0; int count = 0; Byte[] bytes = new Byte[] { 85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101}; startIndex = _generator.GetInt32(-55) % bytes.Length; count = _generator.GetInt32(-55) % (bytes.Length - startIndex) + 1; UTF7Encoding utf7 = new UTF7Encoding(); string str = utf7.GetString(bytes, startIndex, count); }
public void PosTest1() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; int bytesEncodedCount = UTF7.GetBytes(chars, 1, 2, bytes, 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 PosTest2() { int startIndex = 0; int count = 0; Byte[] bytes = new Byte[] { 85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 }; startIndex = _generator.GetInt32(-55) % bytes.Length; count = _generator.GetInt32(-55) % (bytes.Length - startIndex) + 1; UTF7Encoding utf7 = new UTF7Encoding(); string str = utf7.GetString(bytes, startIndex, count); }
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 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); }); }
static string preprocess(string text, string lang) { try { text = ttlServ.UTF8toSGML(text); text = ttlServ.XCES(lang, lang, text); text = ttlServ.SGMLtoUTF7(text); ASCIIEncoding asciiEnc = new ASCIIEncoding(); text = Encoding.UTF8.GetString(UTF7Encoding.Convert(Encoding.UTF7, Encoding.UTF8, asciiEnc.GetBytes(text))); return(text); } catch (Exception e) { return(e.ToString()); } }
public void NegTest3() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; Assert.Throws<ArgumentOutOfRangeException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, -1, 2, bytes, 0); }); }
public string Hash2(string Input, string partialSalt) { string szData = Input + this.baseSalt + partialSalt; byte[] workData = new UTF7Encoding().GetBytes(szData); workData = new MD5CryptoServiceProvider().ComputeHash(workData); StringBuilder sb = new StringBuilder(32); foreach (byte b in workData) { sb.Append(b.ToString("x2")); } return(sb.ToString()); }
private static void VerifyUtf7Encoding(UTF7Encoding encoding, bool allowOptionals) { Assert.Empty(encoding.GetPreamble()); Assert.Equal(new EncoderReplacementFallback(string.Empty), encoding.EncoderFallback); Assert.Equal(1, encoding.DecoderFallback.MaxCharCount); Assert.Equal(984, encoding.DecoderFallback.GetHashCode()); if (allowOptionals) { Assert.Equal(new byte[] { 33 }, encoding.GetBytes("!")); } else { Assert.Equal(new byte[] { 43, 65, 67, 69, 45 }, encoding.GetBytes("!")); } }
/* * Print * * Syntax: * byte[] Print( * FileStream _File // Any open FileStream * long _Offset // Absolute pointer to where string will be written * Encoding _Encoding // Encoding of printed string * string _String // String buffer * ) * byte[] Print( * FileStream _File // Any open FileStream * Encoding _Encoding // Encoding of printed string * string _String // String buffer * ) * byte[] Print( * FileStream _File // Any open FileStream * long _Offset // Absolute pointer to where string will be written * string _String // String buffer * ) * byte[] Print( * FileStream _File // Any open FileStream * string _String // String buffer * ) * * Explanation: * Write a formatted string to a binary file. Null-termination isn't provided. * * This function overwrites _String.Length bytes at _Offset. If the eof (end of file) is * reached, the file is expanded accordingly and the string is completely written. * * _String is always encoded using the _Encoding parameter, regardless of its original * formatting and encoding. * * The overloaded Print() functions without the _Offset parameter will simply print at * the eof (end of file). * * The two overloaded Print() functions without _Encoding always force ASCII printing. * * Return Value: * true on success; false otherwise */ public bool Print(FileStream _File, long _Offset, Encoding _Encoding, string _String) { if (ErrorExists(_File)) { return(false); } try { if (_Encoding == Encoding.ASCII) { ASCIIEncoding enc = new ASCIIEncoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } else if (_Encoding == Encoding.Unicode) { UnicodeEncoding enc = new UnicodeEncoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } else if (_Encoding == Encoding.UTF32) { UTF32Encoding enc = new UTF32Encoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } else if (_Encoding == Encoding.UTF7) { UTF7Encoding enc = new UTF7Encoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } else if (_Encoding == Encoding.UTF8) { UTF8Encoding enc = new UTF8Encoding(); byte[] _Buffer = enc.GetBytes(_String); Write(_File, _Offset, _Buffer, _Buffer.Length); } } catch (Exception Error) { Console.Write("{0}\r\n", Error.Message); return(false); } return(true); }
public static void Main() { // Unicode characters. Char[] chars = new Char[] { '\u0023', // # '\u0025', // % '\u03a0', // Pi '\u03a3' // Sigma }; UTF7Encoding utf7 = new UTF7Encoding(); int byteCount = utf7.GetByteCount(chars, 1, 2); Console.WriteLine( "{0} bytes needed to encode characters.", byteCount ); }
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); }); }
private void encodingToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e) { Encoding encod = null; if (string.IsNullOrEmpty(encodingToolStripComboBox.Text)) { encod = Module.FilesEncoding; } else if (encodingToolStripComboBox.Text.StartsWith("Default", StringComparison.CurrentCultureIgnoreCase)) { encod = Encoding.Default; } else if (encodingToolStripComboBox.Text.Equals("DOS852", StringComparison.CurrentCultureIgnoreCase)) { encod = Encoding.GetEncoding("CP852"); } else if (encodingToolStripComboBox.Text.Equals("ASCII", StringComparison.CurrentCultureIgnoreCase)) { encod = new ASCIIEncoding(); } else if (encodingToolStripComboBox.Text.Equals("Unicode", StringComparison.CurrentCultureIgnoreCase)) { encod = new UnicodeEncoding(); } else if (encodingToolStripComboBox.Text.Equals("UTF7", StringComparison.CurrentCultureIgnoreCase)) { encod = new UTF7Encoding(); } else if (encodingToolStripComboBox.Text.Equals("UTF8", StringComparison.CurrentCultureIgnoreCase)) { encod = new UTF8Encoding(false); } else if (encodingToolStripComboBox.Text.Equals("UTF32", StringComparison.CurrentCultureIgnoreCase)) { encod = new UTF32Encoding(true, false); } else { encod = Module.FilesEncoding; } if (!encod.Equals(this.Encoding)) { this.Encoding = encod; this.OnExtraDiffArgumentsChanged(); } }
public static string ByteArrayToString(byte[] bytes, EncodingType encodingType = EncodingType.Unicode) { int arraySize = bytes.Length; // Redim array to be 2 bytes bigger and fill the last two bytes with 0x00 Array.Resize(ref bytes, arraySize + 2); bytes[arraySize - 1] = 0; bytes[arraySize - 2] = 0; Encoding encoding = null; string result = ""; switch (encodingType) { case EncodingType.ASCII: encoding = new ASCIIEncoding(); break; case EncodingType.Unicode: encoding = new UnicodeEncoding(); break; case EncodingType.UTF7: encoding = new UTF7Encoding(); break; case EncodingType.UTF8: encoding = new UTF8Encoding(); break; case EncodingType.GBK: encoding = Encoding.GetEncoding("GBK");; break; } for (int i = 0; i < bytes.Length; i += 2) { if (bytes[i] == 0 && bytes[i + 1] == 0) { result = encoding.GetString(bytes, 0, i); break; } } return(result); }
public void EnsureDefaults() { Encoding enc = Encoding.Default; string FOO_CONTENT = "foo"; string FOO_DESCRIPTION = "foo description"; StringResource r = new StringResource(FOO_CONTENT); Assert.AreEqual(FOO_CONTENT, r.Content); Assert.AreEqual(enc, r.Encoding); Assert.AreEqual(string.Empty, r.Description); enc = new UTF7Encoding(); r = new StringResource(FOO_CONTENT, enc, FOO_DESCRIPTION); Assert.AreEqual(FOO_CONTENT, r.Content); Assert.AreEqual(enc, r.Encoding); Assert.AreEqual(FOO_DESCRIPTION, r.Description); }
public static IEnumerable<object[]> Equals_TestData() { UTF7Encoding encoding = new UTF7Encoding(); yield return new object[] { encoding, encoding, true }; yield return new object[] { new UTF7Encoding(), new UTF7Encoding(), true }; yield return new object[] { new UTF7Encoding(), new TimeSpan(), false }; yield return new object[] { new UTF7Encoding(), null, false }; yield return new object[] { new UTF7Encoding(), new UTF7Encoding(true), false }; yield return new object[] { new UTF7Encoding(), new UTF7Encoding(false), true }; yield return new object[] { new UTF7Encoding(true), new UTF7Encoding(true), true }; yield return new object[] { new UTF7Encoding(true), new UTF7Encoding(false), false }; yield return new object[] { new UTF7Encoding(false), new UTF7Encoding(false), true }; yield return new object[] { new UTF7Encoding(false), new UTF7Encoding(true), false }; }
public void NegTest3() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF7Encoding UTF7 = new UTF7Encoding(); int byteCount = UTF7.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; Assert.Throws <ArgumentOutOfRangeException>(() => { int bytesEncodedCount = UTF7.GetBytes(chars, -1, 2, bytes, 0); }); }
public static void VerifyUtf7Encoding(UTF7Encoding encoding, bool allowOptionals) { Assert.Empty(encoding.GetPreamble()); Assert.Equal(new EncoderReplacementFallback(string.Empty), encoding.EncoderFallback); Assert.Equal(1, encoding.DecoderFallback.MaxCharCount); Assert.Equal(984, encoding.DecoderFallback.GetHashCode()); if (allowOptionals) { Assert.Equal(new byte[] { 33 }, encoding.GetBytes("!")); } else { Assert.Equal(new byte[] { 43, 65, 67, 69, 45 }, encoding.GetBytes("!")); } }
public void WriteUTF7String_Theory(string testString) { DjvuWriter writer = null; using (MemoryStream stream = new MemoryStream()) using (writer = new DjvuWriter(stream)) { long length = writer.WriteUTF7String(testString); byte[] buffer = stream.GetBuffer(); byte[] testBuffer = new byte[length]; Buffer.BlockCopy(buffer, 0, testBuffer, 0, (int)length); UTF7Encoding encoding = new UTF7Encoding(false); string result = encoding.GetString(testBuffer); Assert.Equal(testString, result); Assert.Equal <long>(length, stream.Position); Assert.Equal <long>(stream.Position, writer.Position); } }
public bool conectorPDV_aut_serial() { alwaysVariables.Serie_Valido = false; if (File.Exists(folderMFDGrand + "\\MD5" + ".enc")) { descryptFile(folderMFDGrand + "\\MD5" + ".enc", "\\MD5"); if (File.Exists(folderMFDGrand + "\\MD5" + ".txt")) { retorno = ""; string mensagem = ""; using (FileStream fs = File.OpenRead(folderMFDGrand + "\\MD5" + ".txt")) { byte[] b = new byte[1024]; UTF7Encoding temp = new UTF7Encoding(true); while (fs.Read(b, 0, b.Length) > 0) { retorno = temp.GetString(b).Replace("\0", "").Trim(); StringReader strReader = new StringReader(retorno); while ((mensagem = strReader.ReadLine()) != null) { if (alwaysVariables.Serie_Hash == mensagem && alwaysVariables.Serie_Hash_Hard == mensagem) { alwaysVariables.Serie_Valido = true; //Verifica serie impressora valido break; } if (alwaysVariables.Serie.Replace("\"", "").Trim() == "\"EMULADOR \"".Replace("\"", "").Trim()) { alwaysVariables.Serie_Valido = true; //Verifica serie impressora valido break; } } } fs.Close(); } } File.Delete(folderMFDGrand + "\\MD5" + ".txt"); } else { conectorPDV_aut(alwaysVariables.CNPJ, folderMFDGrand + "\\MD5" + ".txt"); //cryptografia.encryptFile(alwaysVariables.MD5_Main, "\\MD5", 0); } return(alwaysVariables.Serie_Valido); }
public void TestDirectlyEncoded1() { // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded. string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F"; byte[] UTF7Bytes = null; UTF7Encoding UTF7enc = new UTF7Encoding(); UTF7Bytes = UTF7enc.GetBytes(UniCodeString); Assert.AreEqual(0x61, UTF7Bytes [0], "UTF7 #1"); Assert.AreEqual(0x7A, UTF7Bytes [1], "UTF7 #2"); Assert.AreEqual(0x41, UTF7Bytes [2], "UTF7 #3"); Assert.AreEqual(0x5A, UTF7Bytes [3], "UTF7 #4"); Assert.AreEqual(0x30, UTF7Bytes [4], "UTF7 #5"); Assert.AreEqual(0x39, UTF7Bytes [5], "UTF7 #6"); Assert.AreEqual(0x27, UTF7Bytes [6], "UTF7 #7"); Assert.AreEqual(0x3F, UTF7Bytes [7], "UTF7 #8"); }
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> /// Converts encoding to an encoding object /// </summary> /// <param name="encoding">The encoding type</param> /// <returns>The encoding</returns> public static Encoding GetEncodingFromType(BinaryStringEncoding encoding) { Encoding ret; switch (encoding) { case BinaryStringEncoding.ASCII: ret = new BinaryEncoding(); break; case BinaryStringEncoding.UTF16_BE: ret = new UnicodeEncoding(true, false); break; case BinaryStringEncoding.UTF16_LE: ret = new UnicodeEncoding(false, false); break; case BinaryStringEncoding.UTF32_BE: ret = new UTF32Encoding(true, false); break; case BinaryStringEncoding.UTF32_LE: ret = new UTF32Encoding(false, false); break; case BinaryStringEncoding.UTF8: ret = new UTF8Encoding(); break; case BinaryStringEncoding.UTF7: ret = new UTF7Encoding(); break; case BinaryStringEncoding.EBCDIC_US: ret = Encoding.GetEncoding(37); break; case BinaryStringEncoding.Latin1: ret = Encoding.GetEncoding(28591); break; case BinaryStringEncoding.ShiftJIS: ret = Encoding.GetEncoding(932); break; default: ret = Encoding.GetEncoding((int)encoding); break; } return ret; }
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"); }
/* * GetString * * Syntax: * byte[] GetString( * string Name // Name of file * ) * * Explanation: * Load a text file. * * Return Value: * string of entire file; null otherwise */ public string GetString(Encoding _Encoding, string Name) { byte[] _Buffer = GetBytes(Name); if (_Buffer.Length == 0) { Console.Write("Attempting to read from an uninitialized buffer, aborting...\r\n"); return(null); } try { if (_Encoding == Encoding.ASCII) { ASCIIEncoding enc = new ASCIIEncoding(); return(enc.GetString(_Buffer)); } else if (_Encoding == Encoding.Unicode) { UnicodeEncoding enc = new UnicodeEncoding(); return(enc.GetString(_Buffer)); } else if (_Encoding == Encoding.UTF32) { UTF32Encoding enc = new UTF32Encoding(); return(enc.GetString(_Buffer)); } else if (_Encoding == Encoding.UTF7) { UTF7Encoding enc = new UTF7Encoding(); return(enc.GetString(_Buffer)); } else if (_Encoding == Encoding.UTF8) { UTF8Encoding enc = new UTF8Encoding(); return(enc.GetString(_Buffer)); } } catch (Exception Error) { Console.Write("{0}\r\n", Error.Message); return(null); } return(null); }
public static void Main() { // Create an instance of UTF7Encoding. UTF7Encoding u7 = new UTF7Encoding(true); // Create byte arrays from the same string containing the following characters: // Latin Small Letter Z (U+007A) // Latin Small Letter A (U+0061) // Combining Breve (U+0306) // Latin Small Letter AE With Acute (U+01FD) // Greek Small Letter Beta (U+03B2) String myStr = "za\u0306\u01FD\u03B2"; // Encode the string. byte[] myBArr = new byte[u7.GetByteCount(myStr)]; u7.GetBytes(myStr, 0, myStr.Length, myBArr, 0); // Decode the byte array. Console.WriteLine("The new string is: {0}", u7.GetString(myBArr, 0, myBArr.Length)); }
static Encoding DetectFileEncoding(string filePath) { Encoding encoding = null; using (var reader = new StreamReader(filePath)) encoding = reader.CurrentEncoding; using (var reader = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var bom = new byte[4]; reader.Read(bom, 0, 4); if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) { encoding = new UTF8Encoding(true); } else if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) { encoding = new UTF7Encoding(true); } else if (bom[0] == 0xff && bom[1] == 0xfe) { encoding = new UnicodeEncoding(false, true); } else if (bom[0] == 0xfe && bom[1] == 0xff) { encoding = new UTF8Encoding(false); // encoding = new BigEndianUnicode(true); } else if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) { encoding = new UTF32Encoding(false, true); } else { encoding = new UTF8Encoding(false); } } return encoding; }
public static String GetDocumentSource(ref HTMLDocument doc, EncodingType theDocumentEncoding) { Encoding theEncoding; switch (theDocumentEncoding) { case EncodingType.Auto: IHTMLDocument2 htmldoc = (IHTMLDocument2)doc; String theCharSet = htmldoc.GetCharset(); theEncoding = Encoding.GetEncoding(theCharSet); Debug.Assert(theEncoding != null, "Auto Encoding is null"); break; case EncodingType.ASCII: theEncoding = new ASCIIEncoding(); break; case EncodingType.Unicode: theEncoding = new UnicodeEncoding(); break; case EncodingType.UTF7: theEncoding = new UTF7Encoding(); break; case EncodingType.UTF8: theEncoding = new UTF8Encoding(); break; case EncodingType.WindowsCurrent: theEncoding = Encoding.GetEncoding(0); break; default: theEncoding = Encoding.GetEncoding(0); break; } return(GetDocumentSource(ref doc, theEncoding)); }
/// <summary> /// Converts encoding to an encoding object /// </summary> /// <param name="encoding"></param> /// <returns></returns> protected static Encoding GetEncodingFromType(FilterStringEncoding encoding) { Encoding ret; switch (encoding) { case FilterStringEncoding.ASCII: ret = new BinaryEncoding(); break; case FilterStringEncoding.UTF16_BE: ret = new UnicodeEncoding(true, false); break; case FilterStringEncoding.UTF16_LE: ret = new UnicodeEncoding(false, false); break; case FilterStringEncoding.UTF32_BE: ret = new UTF32Encoding(true, false); break; case FilterStringEncoding.UTF32_LE: ret = new UTF32Encoding(false, false); break; case FilterStringEncoding.UTF8: ret = new UTF8Encoding(); break; case FilterStringEncoding.UTF7: ret = new UTF7Encoding(); break; default: throw new ArgumentException(Properties.Resources.StringDataFrameFilterFactory_GetEncodigFromTypeException); } return(ret); }
/// <summary> /// Returns a Single String converted from the byte array. /// (Converters.cs) /// </summary> public static string ToSingleString(this byte[] value) { if (value == null) { throw new Exception("Value cannot be null."); } Encoding enc = GetEncoding(value); return(enc switch { // ReSharper disable once InconsistentNaming ASCIIEncoding AsciiEncoding => Encoding.ASCII.GetString(value), // ReSharper disable once InconsistentNaming UnicodeEncoding UnicodeEncoding => Encoding.Unicode.GetString(value), // ReSharper disable once InconsistentNaming UTF32Encoding Utf32Encoding => Encoding.UTF32.GetString(value), // ReSharper disable once InconsistentNaming UTF7Encoding Utf7Encoding => Encoding.UTF7.GetString(value), // ReSharper disable once InconsistentNaming UTF8Encoding Utf8Encoding => Encoding.UTF8.GetString(value), _ => Encoding.ASCII.GetString(value) });
public void TestEncodeUnicodeShifted2() { string UniCodeString = "\u0041\u2262\u0391\u002E"; byte[] UTF7Bytes = new byte [10]; int Length = UniCodeString.Length; UTF7Encoding UTF7enc = new UTF7Encoding(); int Cnt = UTF7enc.GetBytes(UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0); //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642 Assert.AreEqual(0x41, UTF7Bytes [0], "UTF7 #1"); Assert.AreEqual(0x2B, UTF7Bytes [1], "UTF7 #2"); Assert.AreEqual(0x49, UTF7Bytes [2], "UTF7 #3"); Assert.AreEqual(0x6D, UTF7Bytes [3], "UTF7 #4"); Assert.AreEqual(0x49, UTF7Bytes [4], "UTF7 #5"); Assert.AreEqual(0x44, UTF7Bytes [5], "UTF7 #6"); Assert.AreEqual(0x6B, UTF7Bytes [6], "UTF7 #7"); Assert.AreEqual(0x51, UTF7Bytes [7], "UTF7 #8"); Assert.AreEqual(0x2D, UTF7Bytes [8], "UTF7 #9"); Assert.AreEqual(0x2E, UTF7Bytes [9], "UTF7 #10"); }
public static string PHPMd5Encode(string input) { string Hashpass = input; // UTF7 Version (probably won't work across languages) System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); UTF7Encoding encoder = new UTF7Encoding(); Byte[] bytes; bytes = encoder.GetBytes(Hashpass); bytes = md5.ComputeHash(bytes); string strHex = string.Empty; foreach (byte b in bytes) { strHex = string.Concat(strHex, String.Format("{0:x2}", b)); } return(strHex); }
public static int GetChatPacketSize(byte[] bytes, int index, int size) { UTF7Encoding uTF7Encoding = new UTF7Encoding(); string text = ""; string text2 = ""; checked { int num = index + 10; while (bytes[num] != 0) { text += uTF7Encoding.GetString(bytes, num, 1); num++; } num++; while (bytes[num] != 0) { text2 += uTF7Encoding.GetString(bytes, num, 1); num++; } return(10 + text.Length + 1 + text2.Length + 1); } }
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 static byte[] StringToByteArray(string str, EncodingType encodingType) { Encoding encoding = null; switch (encodingType) { case EncodingType.ASCII: encoding = new ASCIIEncoding(); break; case EncodingType.Unicode: encoding = new UnicodeEncoding(); break; case EncodingType.UTF7: encoding = new UTF7Encoding(); break; case EncodingType.UTF8: encoding = new UTF8Encoding(); break; } return(encoding.GetBytes(str)); }