public void TestThrowOnInvalid() { UTF8Encoding u = new UTF8Encoding(true, false); byte[] data = new byte [] { 0xC0, 0xAF }; #if NET_2_0 Assert.AreEqual(2, u.GetCharCount(data), "#A0"); string s = u.GetString(data); Assert.AreEqual("\uFFFD\uFFFD", s, "#A1"); #else Assert.AreEqual(0, u.GetCharCount(data), "#A0"); string s = u.GetString(data); Assert.AreEqual(String.Empty, s, "#A1"); #endif data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 }; s = u.GetString(data); #if NET_2_0 Assert.AreEqual(6, s.Length, "#B1"); Assert.AreEqual(0x30, (int)s [0], "#B2"); Assert.AreEqual(0x31, (int)s [1], "#B3"); Assert.AreEqual(0xFFFD, (int)s [2], "#B4"); Assert.AreEqual(0xFFFD, (int)s [3], "#B5"); Assert.AreEqual(0x30, (int)s [4], "#B6"); Assert.AreEqual(0x32, (int)s [5], "#B7"); #else Assert.AreEqual(4, s.Length, "#B1"); Assert.AreEqual(0x30, (int)s [0], "#B2"); Assert.AreEqual(0x31, (int)s [1], "#B3"); Assert.AreEqual(0x30, (int)s [2], "#B4"); Assert.AreEqual(0x32, (int)s [3], "#B5"); #endif }
public static void ValidateUtf8(ReadOnlySpan <byte> utf8Buffer) { try { #if BUILDING_INBOX_LIBRARY s_utf8Encoding.GetCharCount(utf8Buffer); #else if (utf8Buffer.IsEmpty) { return; } unsafe { fixed(byte *srcPtr = utf8Buffer) { s_utf8Encoding.GetCharCount(srcPtr, utf8Buffer.Length); } } #endif } catch (DecoderFallbackException ex) { // We want to be consistent with the exception being thrown // so the user only has to catch a single exception. // Since we already throw InvalidOperationException for mismatch token type, // and while unescaping, using that exception for failure to decode invalid UTF-8 bytes as well. // Therefore, wrapping the DecoderFallbackException around an InvalidOperationException. throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(ex); } }
private void ActGenerate_Click(System.Object sender, System.EventArgs e) { // ----- Generate the XML content using the settings. MemoryStream holdBuffer; Char[] charArray; UTF8Encoding decoder; // ----- Set the XML namespace. SampleDataSet.Tables["Customer"].Namespace = TableNamespace.Text.Trim(); SampleDataSet.Tables["Customer"].Prefix = TablePrefix.Text.Trim(); SampleDataSet.Tables["Order"].Namespace = TableNamespace.Text.Trim(); SampleDataSet.Tables["Order"].Prefix = TablePrefix.Text.Trim(); // ----- Indicate the relationship type. SampleDataSet.Relations[0].Nested = NestChildRecords.Checked; // ----- Clear any existing results. SerializedResults.Clear(); // ----- Build a memory stream to hold the results. holdBuffer = new MemoryStream(8192); SampleDataSet.WriteXml(holdBuffer, (XmlWriteMode)OutputWriteMode.SelectedItem); // ----- Convert it to something displayable. decoder = new UTF8Encoding(); holdBuffer.Seek(0, SeekOrigin.Begin); charArray = new Char[decoder.GetCharCount(holdBuffer.ToArray()) - 1]; decoder.GetDecoder().GetChars(holdBuffer.ToArray(), 0, charArray.Length, charArray, 0); SerializedResults.Text = new string(charArray); }
public bool PosTest1() { bool retVal = true; // Add your scenario description here TestLibrary.TestFramework.BeginScenario("PosTest1: Verify method GetByteCount with a non-null Byte[]"); try { 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 }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, 8); if (charCount != 8) { TestLibrary.TestFramework.LogError("001.1", "Method GetByteCount Err."); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest2() { bool retVal = true; // Add your scenario description here TestLibrary.TestFramework.BeginScenario("PosTest2: Verify method GetByteCount with a null Byte[]"); try { Byte[] bytes = new Byte[] { }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 0, 0); if (charCount != 0) { TestLibrary.TestFramework.LogError("002.1", "Method GetByteCount Err."); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
internal static string PtrToStringUTF8(IntPtr ptr, int size) { if (ptr != IntPtr.Zero && size > 0) { var barray = new byte[size]; // copy utf8 encoded string to byte array Marshal.Copy(ptr, barray, 0, size); // convert the UTF8 encoded string to unicode var utf8_encoding = new UTF8Encoding(); int newsize = utf8_encoding.GetCharCount(barray); var darray = new char[newsize]; Decoder decoder = utf8_encoding.GetDecoder(); int bytes_used, chars_used; bool completed; decoder.Convert( barray, 0, barray.Length, darray, 0, newsize, true, out bytes_used, out chars_used, out completed); return(new string(darray)); } return(null); }
public bool PosTest2() { bool retVal = true; // Add your scenario description here TestLibrary.TestFramework.BeginScenario("PosTest2: Verify method GetByteCount with a null Byte[]"); try { Byte[] bytes = new Byte[] { }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 0, 0); if (charCount != 0) { TestLibrary.TestFramework.LogError("002.1", "Method GetByteCount Err."); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public string stringFromNative(IntPtr nativePtr) { if (nativePtr == IntPtr.Zero) { return(string.Empty); } int i; for (i = 0; Marshal.ReadByte(nativePtr, i) != 0; i++) { } if (i == 0) { return(string.Empty); } if (i > encodedBuffer.Length) { encodedBuffer = new byte[roundUpPowerTwo(i)]; } Marshal.Copy(nativePtr, encodedBuffer, 0, i); int maxCharCount = encoding.GetMaxCharCount(i); if (maxCharCount > decodedBuffer.Length) { int charCount = encoding.GetCharCount(encodedBuffer, 0, i); if (charCount > decodedBuffer.Length) { decodedBuffer = new char[roundUpPowerTwo(charCount)]; } } int chars = encoding.GetChars(encodedBuffer, 0, i, decodedBuffer, 0); return(new string(decodedBuffer, 0, chars)); }
public bool NegTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest1: ArgumentNullException is not thrown when bytes is a null reference"); try { Byte[] bytes = null; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, 8); TestLibrary.TestFramework.LogError("101.1", "ArgumentNullException is not thrown when bytes is a null reference."); retVal = false; } catch (ArgumentNullException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("101.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public void PosTest2() { Byte[] bytes = new Byte[] { }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 0, 0); Assert.Equal(0, charCount); }
public static void Main() { Char[] chars; 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 }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, 13); chars = new Char[charCount]; int charsDecodedCount = utf8.GetChars(bytes, 2, 13, 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(); }
protected override object ReadNext() { if (_nullMap.IsNull(CurrentField)) { return(DBNull.Value); } NpgsqlRowDescription.FieldData field_descr = FieldData; Int32 field_value_size = PGUtil.ReadInt32(Stream) - 4; byte[] buffer = new byte[field_value_size]; PGUtil.CheckedStreamRead(Stream, buffer, 0, field_value_size); char[] charBuffer = new char[UTF8Encoding.GetCharCount(buffer, 0, buffer.Length)]; UTF8Encoding.GetChars(buffer, 0, buffer.Length, charBuffer, 0); try { return (NpgsqlTypesHelper.ConvertBackendStringToSystemType(field_descr.TypeInfo, new string(charBuffer), field_descr.TypeSize, field_descr.TypeModifier)); } catch (InvalidCastException ice) { return(ice); } catch (Exception ex) { return(new InvalidCastException(ex.Message, ex)); } }
private static string ConvertStreamToString(MemoryStream memStream) { int count; byte[] byteArray; char[] charArray; // Read the first 20 bytes from the stream. byteArray = new byte[memStream.Length]; count = memStream.Read(byteArray, 0, 20); UTF8Encoding uniEncoding = new UTF8Encoding(); // Read the remaining bytes, byte by byte. while (count < memStream.Length) { byteArray[count++] = Convert.ToByte(memStream.ReadByte()); } // Decode the byte array into a char array // and write it to the console. charArray = new char[uniEncoding.GetCharCount( byteArray, 0, count)]; uniEncoding.GetDecoder().GetChars( byteArray, 0, count, charArray, 0); string s = new string(charArray); return(s); }
public bool NegTest4() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest4: ArgumentOutOfRangeException is not thrown when index and count do not denote a valid range in bytes."); try { 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 }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, bytes.Length); TestLibrary.TestFramework.LogError("104.1", "ArgumentNullException is not thrown whenindex and count do not denote a valid range in bytes."); retVal = false; } catch (ArgumentOutOfRangeException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("104.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest1() { bool retVal = true; // Add your scenario description here TestLibrary.TestFramework.BeginScenario("PosTest1: Verify method GetByteCount with a non-null Byte[]"); try { 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}; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, 8); if (charCount != 8) { TestLibrary.TestFramework.LogError("001.1", "Method GetByteCount Err."); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public static string _fnDecryptStr(string sData) { Decoder decoder = new UTF8Encoding().GetDecoder(); byte[] bytes = Convert.FromBase64String(sData); char[] chars = new char[decoder.GetCharCount(bytes, 0, bytes.Length)]; decoder.GetChars(bytes, 0, bytes.Length, chars, 0); return(new string(chars)); }
public void NegTest1() { Byte[] bytes = null; UTF8Encoding utf8 = new UTF8Encoding(); Assert.Throws<ArgumentNullException>(() => { int charCount = utf8.GetCharCount(bytes, 2, 8); }); }
/// <summary>Base64 string decoder</summary> /// <param name="text">The text string to decode</param> /// <returns>The decoded string</returns> public static string Base64Decode(this string text) { Decoder decoder = new UTF8Encoding().GetDecoder(); byte[] bytes = Convert.FromBase64String(text); char[] chars = new char[decoder.GetCharCount(bytes, 0, bytes.Length)]; decoder.GetChars(bytes, 0, bytes.Length, chars, 0); return(new string(chars)); }
public void NegTest1() { Byte[] bytes = null; UTF8Encoding utf8 = new UTF8Encoding(); Assert.Throws <ArgumentNullException>(() => { int charCount = utf8.GetCharCount(bytes, 2, 8); }); }
public static string DecryptData(string encryptedText) { var decode = new UTF8Encoding().GetDecoder(); var bytesOfEncryptedText = Convert.FromBase64String(encryptedText); var charCount = decode.GetCharCount(bytesOfEncryptedText, 0, bytesOfEncryptedText.Length); var decodedChars = new char[charCount]; decode.GetChars(bytesOfEncryptedText, 0, bytesOfEncryptedText.Length, decodedChars, 0); return(new String(decodedChars)); }
/// <summary> /// Returns true if the given data is a valid UTF8 string. /// </summary> public static bool IsValidUTF8(byte[] data, int index, int count) { try { Encoding encoding = new UTF8Encoding(true, true); encoding.GetCharCount(data, index, count); return(true); } catch (DecoderFallbackException) { return(false); } }
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}; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, 8); Assert.Equal(8, charCount); }
internal static string ReadString(byte[] data, int offset) { var utf8 = new UTF8Encoding(); var len = ReadUShort(data, offset); offset += Constants.Bit16Sz; var strdata = new char[utf8.GetCharCount(data, offset, len)]; utf8.GetChars(data, offset, len, strdata, 0); return(new string(strdata)); }
protected string ConvertBytesToUTF8(string str) { UTF8Encoding utf8 = new UTF8Encoding(); byte[] bytes = Convert.FromBase64String(str); int charCount = utf8.GetCharCount(bytes); Char[] chars = new Char[charCount]; utf8.GetChars(bytes, 0, bytes.Length, chars, 0); return(new String(chars)); }
[InlineData("25249-0.txt")] // Chinese, UTF-8 (primarily 3-byte sequences) public void GetUtf16CharCount_UsingInboxEncoder(string resourceName) { ReadOnlySpan <byte> utf8Text = ReadTestResource(resourceName); // Call UTF8Encoding.GetCharCount once to ensure it's JITted _nonReplacingEncoder.GetCharCount(utf8Text); // Perform perf test foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { _nonReplacingEncoder.GetCharCount(utf8Text); } } } }
private static string Base64Decode(string data) { try { Decoder utf8Decode = new UTF8Encoding().GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(data); char[] decoded_char = new char[(utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length))]; utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); return(new String(decoded_char)); } catch (Exception) { return(null); } }
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}; UTF8Encoding utf8 = new UTF8Encoding(); Assert.Throws<ArgumentOutOfRangeException>(() => { int charCount = utf8.GetCharCount(bytes, 2, -1); }); }
/// <summary> /// 获取utf8字符串 /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static string UTF8(byte[] bytes) { #if UNITY_WP8 UTF8Encoding coding = new UTF8Encoding(); int charCount = coding.GetCharCount(bytes); Char[] chars = new Char[charCount]; int charsDecodedCount = coding.GetChars(bytes, 0, bytes.Length, chars, 0); return(chars.ToString()); #else return(System.Text.Encoding.UTF8.GetString(bytes)); #endif }
/// <summary> /// Note that "maxLength" only limits the number of characters in a string, not its size in bytes. /// </summary> /// <returns>"string.Empty" if value > "maxLength"</returns> public string GetString(int maxLength) { ushort size = GetUShort(); if (size == 0) { return(null); } int actualSize = size - 1; if (actualSize >= NetDataWriter.StringBufferMaxLength) { return(null); } ArraySegment <byte> data = GetBytesSegment(actualSize); return((maxLength > 0 && _uTF8Encoding.GetCharCount(data.Array, data.Offset, data.Count) > maxLength) ? string.Empty : _uTF8Encoding.GetString(data.Array, data.Offset, data.Count)); }
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 }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, 8); Assert.Equal(8, charCount); }
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 }; UTF8Encoding utf8 = new UTF8Encoding(); Assert.Throws <ArgumentOutOfRangeException>(() => { int charCount = utf8.GetCharCount(bytes, 2, bytes.Length); }); }
public string convstring(Byte[] data) { char[] strdata; string ret; UTF8Encoding utf8; utf8 = new UTF8Encoding(); strdata = new char[utf8.GetCharCount(data, 0, data.Length)]; utf8.GetChars(data, 0, data.Length, strdata, 0); ret = new string(strdata); return(ret); }
public string decryptSimple(string stringToDec) { try { System.Text.Decoder utf8decode = new UTF8Encoding().GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(stringToDec); char[] decoded_char = new char[utf8decode.GetCharCount(todecode_byte, 0, todecode_byte.Length)]; utf8decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); return(new string(decoded_char)); } catch (Exception ex) { throw ex; } }
public static void Main() { 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 }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, 8); Console.WriteLine( "{0} characters needed to decode bytes.", charCount ); }
/// <summary> /// Decodes the content field /// </summary> /// <param name="content">string</param> /// <returns>string</returns> private string DecodeContentField(string content) { if (!string.IsNullOrEmpty(content)) { byte[] documentBytes = Convert.FromBase64String(content); if (documentBytes.Length > 0) { Decoder utfDecode = new UTF8Encoding().GetDecoder(); var decodedChar = new char[utfDecode.GetCharCount(documentBytes, 0, documentBytes.Length)]; utfDecode.GetChars(documentBytes, 0, documentBytes.Length, decodedChar, 0); content = new string(decodedChar); } } return(content); }
public bool NegTest4() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest4: ArgumentOutOfRangeException is not thrown when index and count do not denote a valid range in bytes."); try { 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}; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, bytes.Length); TestLibrary.TestFramework.LogError("104.1", "ArgumentNullException is not thrown whenindex and count do not denote a valid range in bytes."); retVal = false; } catch (ArgumentOutOfRangeException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("104.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public bool NegTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest1: ArgumentNullException is not thrown when bytes is a null reference"); try { Byte[] bytes = null; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 2, 8); TestLibrary.TestFramework.LogError("101.1", "ArgumentNullException is not thrown when bytes is a null reference."); retVal = false; } catch (ArgumentNullException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("101.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }