public bool PosTest2() { bool retVal = true; // Add your scenario description here TestLibrary.TestFramework.BeginScenario("PosTest2: Verify method GetChars with null chars"); try { Byte[] bytes; Char[] chars = new Char[] { }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 0, 0); bytes = new Byte[byteCount]; int charsEncodedCount = utf8.GetChars(bytes, 0, 0, chars, 0); if (charsEncodedCount != 0) { TestLibrary.TestFramework.LogError("002.1", "Method GetChars Err."); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.1", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public void PosTest2() { Byte[] bytes; Char[] chars = new Char[] { }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 0, 0); bytes = new Byte[byteCount]; int charsEncodedCount = utf8.GetChars(bytes, 0, 0, chars, 0); Assert.Equal(0, charsEncodedCount); }
public void PosTest1() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; int charsEncodedCount = utf8.GetChars(bytes, 1, 2, chars, 0); }
/// <summary> /// Decode the UTF-8 string beginning at pos within the buffer. When complete, pos will point /// to the byte immediately after the string in the buffer. /// </summary> /// <param name="src"></param> /// <param name="pos"></param> /// <returns></returns> public static string DecodeString(byte[] src, ref int pos) { var encoder = new UTF8Encoding(); var sb = new StringBuilder(); int length = Frame.DecodeInt16(src, ref pos); if (length > 0) { int start = pos; pos += length; char[] chars = encoder.GetChars(src, start, length); sb.Append(chars); } return(sb.ToString()); }
/// <summary> /// ReadLine - Read buffer upto first \r\n chars /// </summary> /// <returns>Return string containing: Bytes upto first \r\n characters. /// or remainder of buffer if end of buffer is reached but no \r\n found /// or empty string if \r\n found at first position /// or null if at end of buffer on call /// </returns> private string ReadLine() { int startPos = m_curPos; int lastBufPos = m_length - 1; int endPos = m_curPos; if (m_curPos == m_length) { return(null); } int idx = Array.IndexOf(m_buffer, '\r', startPos); if (idx >= 0) { if (idx < lastBufPos && m_buffer[idx + 1] == '\n') { m_curPos = idx + 2; } else { m_curPos = idx; } endPos = idx; } else { m_curPos = lastBufPos; endPos = lastBufPos; } if (startPos == endPos) { return(""); } int length = endPos - startPos; byte[] tempBuff = new byte[length]; Array.Copy(m_buffer, startPos, tempBuff, 0, length); return(new string(m_utf8Encoding.GetChars(tempBuff), 0, length)); }
public void NegTest1() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 1, 2); bytes = null; Assert.Throws<ArgumentNullException>(() => { int charsEncodedCount = utf8.GetChars(bytes, 1, 2, chars, 0); }); }
public void NegTest6() { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; Assert.Throws <ArgumentOutOfRangeException>(() => { int charsEncodedCount = utf8.GetChars(bytes, 2, 2, chars, 1); }); }
public static string ToString(sbyte[] sbytes) { var bytes = new byte[sbytes.Length]; int i; for (i = 0; i < bytes.Length && sbytes[i] != '\0'; i++) { bytes[i] = (byte)sbytes[i]; } var bytesUntilNull = new byte[i]; Array.Copy(bytes, bytesUntilNull, i); var encoding = new UTF8Encoding(); return(new string(encoding.GetChars(bytesUntilNull))); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Processes the directory entry. /// </summary> /// <param name="fId">set to <c>true</c> if its an ID entry, <c>false</c> if its a Name /// entry.</param> /// ------------------------------------------------------------------------------------ private void ProcessDirectoryEntry(bool fId) { long nBasePos = m_stream.Position; int id; int nameRVA; if (fId) { id = m_reader.ReadInt32(); m_TypeStack.Add(id); } else { nameRVA = m_reader.ReadInt32() & 0x7FFFFFFF; m_stream.Position = m_BaseAddress + nameRVA; short nStringLen = m_reader.ReadInt16(); UTF8Encoding encoding = new UTF8Encoding(); byte[] buffer = UnicodeEncoding.Convert(new UnicodeEncoding(), encoding, m_reader.ReadBytes(2 * nStringLen)); string name = new string(encoding.GetChars(buffer)); m_TypeStack.Add(name); m_stream.Position = nBasePos + 4; } int value = m_reader.ReadInt32(); bool fSubDir = (value & 0x80000000) != 0; int nVirtualAddress = value & 0x7FFFFFFF; m_stream.Position = m_BaseAddress + nVirtualAddress; if (fSubDir) { ProcessResourceDirectoryTable(); } else { ProcessDataEntry(); } m_TypeStack.RemoveAt(m_TypeStack.Count - 1); m_stream.Position = nBasePos + 8; }
public string getstring(Byte[] data, uint dp) { ushort len; char[] strdata; string ret; UTF8Encoding utf8; utf8 = new UTF8Encoding(); len = getushort(data, dp); dp += (uint)proto.BIT16SZ; strdata = new char[utf8.GetCharCount(data, (int)dp, (int)len)]; utf8.GetChars(data, (int)dp, (int)len, strdata, 0); ret = new string(strdata); return(ret); }
public static string Decode(string data) { string str2 = ""; try { var decoder = new UTF8Encoding().GetDecoder(); var bytes = Convert.FromBase64String(data); var chars = new char[decoder.GetCharCount(bytes, 0, bytes.Length)]; decoder.GetChars(bytes, 0, bytes.Length, chars, 0); var str = new string(chars); str2 = str; } catch (Exception ex) { Console.WriteLine(ex.Message); } return(str2); }
private static ByteStringContext.InternalScope UnlikelyGetLowerUnicode(ByteStringContext byteStringContext, byte *str, int size, out Slice loweredKey) { var maxCharCount = Encoding.GetMaxCharCount(size); var bufferSize = maxCharCount * sizeof(char); using (byteStringContext.Allocate(bufferSize, out var ptr)) { var chars = (char *)ptr.Ptr; var charCount = Encoding.GetChars(str, size, chars, maxCharCount); for (int i = 0; i < charCount; i++) { chars[i] = char.ToLowerInvariant(chars[i]); } var release = byteStringContext.From(chars, charCount, ByteStringType.Immutable, out var result); loweredKey = new Slice(result); return(release); } }
public static Boolean TryDecodeBase64(this String Text, out String Decoded) { try { var utf8Decoder = new UTF8Encoding().GetDecoder(); var bytes = Convert.FromBase64String(Text); var decodedChars = new Char[utf8Decoder.GetCharCount(bytes, 0, bytes.Length)]; utf8Decoder.GetChars(bytes, 0, bytes.Length, decodedChars, 0); Decoded = new String(decodedChars); return(true); } catch (Exception e) { } Decoded = default; return(false); }
public void Received(object sender, SerialDataReceivedEventArgs e) { byte currentByte; while (port.BytesToRead > 0) { currentByte = (byte)port.ReadByte(); // ensures that byte is always consumed switch (currentByte) { case START_OF_TEXT: // starts new message, even if in the middle of one. newMessage(); messageLength = (byte)port.ReadByte(); break; case END_OF_TEXT: // do something with what we've got. if (messageStarted) // normal { // will need to chop off last few bytes for CRC, if implemented. var receivedMessage = new string(encoder.GetChars(messageIn, 0, messageIn.Length)); // converts filled char[] elements to string (i.e. shortens it) useMessage(receivedMessage); // normal message completion. } messageReset(); // Deal with incorrectly ended messages break; default: // add characters. if (!messageStarted) // ignore it { break; // current byte is already consumed. } messageIn[currentPositionInMessage] = currentByte; currentPositionInMessage++; break; } if (currentPositionInMessage >= MAX_MESSAGE_SIZE || messageLength > MAX_MESSAGE_SIZE) // error. Reset. { messageReset(); // error. Reset. } } }
// Added by RPH for Borland 2006 // The assumption is that we will decode using UTF8 based on // "The Firebird Book" by Helen Borrie. public int GetClob(short index, ref char[] buffer, ref int nullInd, int iLength) { this.CheckPosition(); this.CheckIndex(index); this.GetIsNull(index, ref nullInd); if (nullInd == 0) { // Not null, get data int realLength = iLength; byte[] byteArray = this.row[index].GetBinary(); if (iLength > byteArray.Length) { // Fix length realLength = byteArray.Length; } UTF8Encoding encoder = new UTF8Encoding(); encoder.GetChars(byteArray, 0, realLength, buffer, 0); } return(0); }
public override string ReadString() { int len = ReadEncodedInt(); if (len == 0) { return(string.Empty); } if (len < 0 || len > 32767) { throw new IOException("Invalid binary file: string len " + len); } if (charBuf.Length < len) { charBuf = new char[len]; } int n = utf.GetChars(stream.Buf, stream.Pos, len, charBuf, 0); // looks thread-safe to me stream.Skip(len); return(new string(charBuf, 0, n)); }
public static string Base64Decode(string sData) { try { Decoder decoder = new UTF8Encoding().GetDecoder(); byte[] numArray = Convert.FromBase64String(sData); byte[] bytes1 = numArray; int index = 0; int length1 = numArray.Length; char[] chArray = new char[decoder.GetCharCount(bytes1, index, length1)]; byte[] bytes2 = numArray; int byteIndex = 0; int length2 = numArray.Length; char[] chars = chArray; int charIndex = 0; decoder.GetChars(bytes2, byteIndex, length2, chars, charIndex); return(new string(chArray)); } catch (Exception ex) { throw new Exception("Error in base64Decode" + ex.Message); } }
/// <summary> /// Reads a string from the stream (using UTF8); supported wire-types: String /// </summary> public string ReadString() { if (wireType == WireType.String) { int bytes = (int)ReadUInt32Variant(false); if (bytes == 0) { return(""); } if (available < bytes) { Ensure(bytes, true); } #if MF byte[] tmp; if (ioIndex == 0 && bytes == ioBuffer.Length) { // unlikely, but... tmp = ioBuffer; } else { tmp = new byte[bytes]; Helpers.BlockCopy(ioBuffer, ioIndex, tmp, 0, bytes); } string s = new string(encoding.GetChars(tmp)); #else string s = encoding.GetString(ioBuffer, ioIndex, bytes); #endif s = Intern(s); available -= bytes; position += bytes; ioIndex += bytes; return(s); } throw CreateException(); }
public bool NegTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest2: ArgumentNullException is not thrown when chars is a null reference "); try { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; chars = null; int charsEncodedCount = utf8.GetChars(bytes, 1, 2, chars, 0); TestLibrary.TestFramework.LogError("102.1", "ArgumentNullException is not thrown when chars is a null reference "); retVal = false; } catch (ArgumentNullException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("102.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
private static List <EscPosCmd> Convertstrings(List <EscPosCmd> escposlist) { Boolean prtutf8 = false; Encoding prtencoding = Encoding.Default; Dictionary <byte, string> prtembedded = new Dictionary <byte, string>(); Dictionary <string, string> prtreplaced = new Dictionary <string, string>(); UTF8Encoding utf8encoding = new UTF8Encoding();; Encoding vfdencoding = Encoding.Default; Dictionary <byte, string> vfdembedded = new Dictionary <byte, string>(); Dictionary <string, string> vfdreplaced = new Dictionary <string, string>(); try { if (initialcodepage < 0) { initialcodepage = CultureInfo.CurrentCulture.TextInfo.OEMCodePage; initialkanjion = !Encoding.GetEncoding(initialcodepage).IsSingleByte; } } catch { return(escposlist); } int prtcodepage = initialcodepage; int prtICS = initialICS; int vfdcodepage = initialcodepage; int vfdICS = initialICS; Boolean prtkanjimode = initialkanjion; Boolean vfdkanjimode = initialkanjion; try { if (prtcodepage < 0x100) { prtembedded = EscPosDecoder.GetEmbeddedESCtCodePage(prtcodepage); } else { prtencoding = Encoding.GetEncoding(prtcodepage); } if (prtICS != 0) { prtreplaced = EscPosDecoder.s_StringESCRICS[(byte)prtICS]; } if (vfdcodepage < 0x100) { vfdembedded = EscPosDecoder.GetEmbeddedESCtCodePage(vfdcodepage); } else { prtencoding = Encoding.GetEncoding(vfdcodepage); } if ((vfdICS >= 1) && (vfdICS <= 17)) { vfdreplaced = EscPosDecoder.s_StringESCRICS[(byte)vfdICS]; } else { vfdICS = 0; } } catch { return(escposlist); } foreach (EscPosCmd item in escposlist) { switch (item.cmdtype) { case EscPosCmdType.EscInitialize: prtcodepage = initialcodepage; prtICS = initialICS; prtkanjimode = initialkanjion; try { if (prtcodepage < 0x100) { prtembedded = EscPosDecoder.GetEmbeddedESCtCodePage(prtcodepage); } else { prtencoding = Encoding.GetEncoding(prtcodepage); } if (prtICS != 0) { prtreplaced = EscPosDecoder.s_StringESCRICS[(byte)prtICS]; } } catch { } break; case EscPosCmdType.EscSelectInternationalCharacterSet: try { if (EscPosDecoder.s_StringESCRICS.ContainsKey(item.cmddata[2])) { prtICS = item.cmddata[2]; if (prtICS != 0) { prtreplaced = EscPosDecoder.s_StringESCRICS[(byte)prtICS]; } } } catch { } break; case EscPosCmdType.EscSelectCharacterCodeTable: try { if (EscPosDecoder.PrtESCtCodePage.TryGetValue(item.cmddata[2], out prtcodepage)) { if (prtcodepage < 0x100) { prtembedded = EscPosDecoder.GetEmbeddedESCtCodePage(prtcodepage); } else { prtencoding = Encoding.GetEncoding(prtcodepage); } } } catch { } break; case EscPosCmdType.FsSelectKanjiCharacterMode: prtkanjimode = true; break; case EscPosCmdType.FsCancelKanjiCharacterMode: prtkanjimode = false; break; case EscPosCmdType.FsSelectCharacterEncodeSystem: { byte m = item.cmddata[6]; prtutf8 = ((m == 2) || (m == 50)); } break; case EscPosCmdType.PrtPrintables: try { List <string> listwork = new List <string>(); if (prtutf8) { listwork.AddRange(utf8encoding.GetChars(item.cmddata).Select(c => c.ToString())); } else if (prtcodepage < 0x100) { listwork.AddRange(item.cmddata.Select(c => prtembedded[c])); } else { listwork.AddRange(prtencoding.GetChars(item.cmddata).Select(c => c.ToString())); } item.paramdetail = string.Join("", (prtICS == 0 ? listwork : listwork.Select(s => (prtreplaced.ContainsKey(s) ? prtreplaced[s] : s)))); } catch { } break; case EscPosCmdType.VfdEscInitialize: vfdcodepage = initialcodepage; vfdICS = initialICS; vfdkanjimode = initialkanjion; try { if (vfdcodepage < 0x100) { vfdembedded = EscPosDecoder.GetEmbeddedESCtCodePage(vfdcodepage); } else { prtencoding = Encoding.GetEncoding(vfdcodepage); } } catch { } break; case EscPosCmdType.VfdEscSelectInternationalCharacterSet: if (EscPosDecoder.s_StringESCRICS.ContainsKey(item.cmddata[2])) { vfdICS = item.cmddata[2]; if ((vfdICS >= 1) && (vfdICS <= 17)) { vfdreplaced = EscPosDecoder.s_StringESCRICS[(byte)vfdICS]; } else { vfdICS = 0; } } break; case EscPosCmdType.VfdEscSelectCharacterCodeTable: try { if (EscPosDecoder.VfdESCtCodePage.TryGetValue(item.cmddata[2], out vfdcodepage)) { if (vfdcodepage < 0x100) { vfdembedded = EscPosDecoder.GetEmbeddedESCtCodePage(vfdcodepage); } else { prtencoding = Encoding.GetEncoding(vfdcodepage); } } } catch { } break; case EscPosCmdType.VfdUsKanjiCharacterModeOnOff: vfdkanjimode = item.cmddata[6] switch { 0 => false, 48 => false, 1 => true, 49 => true, _ => vfdkanjimode, }; break; case EscPosCmdType.VfdDisplayables: try { List <string> listwork = new List <string>(); if (vfdcodepage < 0x100) { listwork.AddRange(item.cmddata.Select(c => vfdembedded[c])); } else { listwork.AddRange(vfdencoding.GetChars(item.cmddata).Select(c => c.ToString())); } item.paramdetail = string.Join("", (vfdICS == 0 ? listwork : listwork.Select(s => (vfdreplaced.ContainsKey(s) ? vfdreplaced[s] : s)))); } catch { } break; } ; } return(escposlist); }
private int Dispose(string keystring, bool run) { int num18 = 0; if (keystring.Length == 0) { return(0); } try { byte[] buffer1 = Convert.FromBase64String(keystring); byte[] buffer2 = new byte[0x10] { 0x21, 0x53, 0x1b, 0x5f, 0x1c, 0x54, 0xc5, 0xa9, 0x27, 0x5d, 0x4b, 0x69, 0x52, 0x61, 0x31, 0x2c }; MemoryStream stream1 = new MemoryStream(buffer1); RijndaelManaged managed1 = new RijndaelManaged(); CryptoStream stream2 = new CryptoStream(stream1, managed1.CreateDecryptor(buffer2, buffer2), CryptoStreamMode.Read); byte[] buffer3 = new byte[0x1000]; int num1 = stream2.Read(buffer3, 0, buffer3.Length); System.Text.Decoder decoder1 = new UTF8Encoding().GetDecoder(); char[] chArray1 = new char[decoder1.GetCharCount(buffer3, 0, num1)]; int num2 = decoder1.GetChars(buffer3, 0, num1, chArray1, 0); string text1 = new string(chArray1, 0, num2); char[] chArray2 = new char[1] { '|' }; string[] textArray1 = text1.Split(chArray2); int num3 = textArray1.Length; DateTime time1 = DateTime.Today; CultureInfo info1 = CultureInfo.CurrentCulture; if (run) { if (num3 < 11) { return(0); } string text2 = (num3 > 0) ? textArray1[0] : ""; string text3 = (num3 > 1) ? textArray1[1] : ""; int num4 = (num3 > 2) ? this.ParseInt(textArray1[2]) : -1; int num5 = (num3 > 3) ? this.ParseInt(textArray1[3]) : -1; int num6 = (num3 > 9) ? this.ParseInt(textArray1[9]) : 7; string text4 = (num3 > 10) ? textArray1[10] : "E"; int num7 = (num3 > 11) ? this.ParseInt(textArray1[11]) : 0x270f; int num8 = (num3 > 12) ? this.ParseInt(textArray1[12]) : 1; int num9 = (num3 > 13) ? this.ParseInt(textArray1[13]) : 1; int num10 = (num3 > 14) ? this.ParseInt(textArray1[14]) : 360; AssemblyName name1 = Assembly.GetExecutingAssembly().GetName(); if ((text3.Length > 0) && (string.Compare(text3, name1.Name, true, info1) != 0)) { return(0); } if (num4 >= 0) { if (name1.Version.Major > num4) { return(0); } if (((num5 >= 0) && (name1.Version.Major == num4)) && (name1.Version.Minor > num5)) { return(0); } } if (text4[0] == 'E') { return(0); } if ((text4[0] == 'R') && ((DiagramView.myVersionAssembly == null) || (string.Compare(text2, DiagramView.myVersionAssembly.GetName().Name, true, info1) != 0))) { return(0); } DateTime time2 = new DateTime(num7, num8, num9); if (time1.AddDays((double)num10) <= time2) { return(4); } if (time1.AddDays(7) <= time2) { return(6); } if (time1.AddDays((double)-num6) <= time2) { return(5); } goto Label_0522; } string text5 = (num3 > 1) ? textArray1[1] : ""; int num11 = (num3 > 2) ? this.ParseInt(textArray1[2]) : -1; int num12 = (num3 > 3) ? this.ParseInt(textArray1[3]) : -1; string text6 = (num3 > 4) ? textArray1[4] : ""; string text7 = (num3 > 5) ? textArray1[5] : ""; int num13 = (num3 > 6) ? this.ParseInt(textArray1[6]) : 1; int num14 = (num3 > 7) ? this.ParseInt(textArray1[7]) : 1; int num15 = (num3 > 8) ? this.ParseInt(textArray1[8]) : 1; DateTime time3 = new DateTime(num13, num14, num15); int num16 = (num3 > 9) ? this.ParseInt(textArray1[9]) : 7; string text8 = (num3 > 10) ? textArray1[10] : "E"; int num17 = (num3 > 14) ? this.ParseInt(textArray1[14]) : 360; AssemblyName name2 = Assembly.GetExecutingAssembly().GetName(); if ((text5.Length > 0) && (string.Compare(text5, name2.Name, true, info1) != 0)) { return(0); } if (num11 >= 0) { if (name2.Version.Major > num11) { return(0); } if (((num12 >= 0) && (name2.Version.Major == num11)) && (name2.Version.Minor > num12)) { return(0); } } if ((text6.Length > 0) && (string.Compare(Environment.MachineName, text6, true, info1) != 0)) { return(0); } if ((text7.Length > 0) && (string.Compare(Environment.UserName, text7, true, info1) != 0)) { return(0); } if (text8[0] == 'B') { if (time1.AddDays((double)num17) <= time3) { return(4); } if (time1.AddDays(7) <= time3) { return(6); } if (time1.AddDays((double)-num16) <= time3) { return(5); } goto Label_0522; } if (time1.AddDays(7) <= time3) { return(2); } if (time1.AddDays((double)-num16) > time3) { goto Label_0522; } num18 = 1; } catch (Exception) { } return(num18); Label_0522: return(0); }
public static int ToChars(byte[] buffer, int offset, int count, char[] chars, int charOffset) { return(UTF8Encoding.GetChars(buffer, offset, count, chars, charOffset)); }
public bool TryReadChars(char[] chars, int offset, int count, out int actual) { if (_type == ValueHandleType.Unicode) { return(TryReadUnicodeChars(chars, offset, count, out actual)); } if (_type != ValueHandleType.UTF8) { actual = 0; return(false); } int charOffset = offset; int charCount = count; byte[] bytes = _bufferReader.Buffer; int byteOffset = _offset; int byteCount = _length; while (true) { while (charCount > 0 && byteCount > 0) { byte b = bytes[byteOffset]; if (b >= 0x80) { break; } chars[charOffset] = (char)b; byteOffset++; byteCount--; charOffset++; charCount--; } if (charCount == 0 || byteCount == 0) { break; } int actualByteCount; int actualCharCount; UTF8Encoding encoding = new UTF8Encoding(false, true); try { // If we're asking for more than are possibly available, or more than are truly available then we can return the entire thing if (charCount >= encoding.GetMaxCharCount(byteCount) || charCount >= encoding.GetCharCount(bytes, byteOffset, byteCount)) { actualCharCount = encoding.GetChars(bytes, byteOffset, byteCount, chars, charOffset); actualByteCount = byteCount; } else { Decoder decoder = encoding.GetDecoder(); // Since x bytes can never generate more than x characters this is a safe estimate as to what will fit actualByteCount = Math.Min(charCount, byteCount); // We use a decoder so we don't error if we fall across a character boundary actualCharCount = decoder.GetChars(bytes, byteOffset, actualByteCount, chars, charOffset); // We might've gotten zero characters though if < 3 chars were requested // (e.g. 1 char requested, 1 char in the buffer represented in 3 bytes) while (actualCharCount == 0) { // Request a few more bytes to get at least one character actualCharCount = decoder.GetChars(bytes, byteOffset + actualByteCount, 1, chars, charOffset); actualByteCount++; } // Now that we actually retrieved some characters, figure out how many bytes it actually was actualByteCount = encoding.GetByteCount(chars, charOffset, actualCharCount); } } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateEncodingException(bytes, byteOffset, byteCount, exception)); } // Advance byteOffset += actualByteCount; byteCount -= actualByteCount; charOffset += actualCharCount; charCount -= actualCharCount; } _offset = byteOffset; _length = byteCount; actual = (count - charCount); return(true); }
public ProtectedString Insert(int iStart, string strInsert) { if (iStart < 0) { throw new ArgumentOutOfRangeException("iStart"); } if (strInsert == null) { throw new ArgumentNullException("strInsert"); } if (strInsert.Length == 0) { return(this); } // Only operate directly with strings when m_bIsProtected is // false, not in the case of non-null m_strPlainText, because // the operation creates a new sequence in memory if (!m_bIsProtected) { return(new ProtectedString(false, ReadString().Insert( iStart, strInsert))); } UTF8Encoding utf8 = StrUtil.Utf8; byte[] pb = ReadUtf8(); char[] v = utf8.GetChars(pb); char[] vNew; try { if (iStart > v.Length) { throw new ArgumentOutOfRangeException("iStart"); } char[] vIns = strInsert.ToCharArray(); vNew = new char[v.Length + vIns.Length]; Array.Copy(v, 0, vNew, 0, iStart); Array.Copy(vIns, 0, vNew, iStart, vIns.Length); Array.Copy(v, iStart, vNew, iStart + vIns.Length, v.Length - iStart); } finally { MemUtil.ZeroArray <char>(v); MemUtil.ZeroByteArray(pb); } byte[] pbNew = utf8.GetBytes(vNew); ProtectedString ps = new ProtectedString(m_bIsProtected, pbNew); Debug.Assert(utf8.GetString(pbNew, 0, pbNew.Length) == ReadString().Insert(iStart, strInsert)); MemUtil.ZeroArray <char>(vNew); MemUtil.ZeroByteArray(pbNew); return(ps); }
public bool NegTest7() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest7: ArgumentException is not thrown when chars does not have enough capacity from charIndex to the end of the array to accommodate the resulting characters"); try { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; int charsEncodedCount = utf8.GetChars(bytes, 1, 2, chars, chars.Length); TestLibrary.TestFramework.LogError("107.1", "ArgumentException is not thrown when byteIndex is not a valid index in bytes."); retVal = false; } catch (ArgumentException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("107.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
/// <summary> /// 转化文本的编码 /// 获取了中间的string字符串 /// </summary> /// <param name="path"></param> /// <param name="targetEncoding"></param> private static void SwitchEncoding(string path, Encoding targetEncoding) { // 获取编码 MyEncodingType myEncodingType = GetEncode(path); // 获取源文本 string sourceStr = String.Empty; using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { // 获取原文本 using (BinaryReader rd = new BinaryReader(fs, Encoding.UTF8)) // 这里的编码并没有什么作用,只是为了第三个参数,读取后不关闭文件流 { // 获取解码器 Decoder de = null; switch (myEncodingType) { case MyEncodingType.ASCII_UTF8withoutBom: // 源文本全为英文,也采用UTF8来解码 de = new UTF8Encoding(false).GetDecoder(); break; case MyEncodingType.ANSI_Default: if (targetEncoding == Encoding.ASCII) { throw new Exception("当前编码,无法转为ASCII码"); } de = Encoding.Default.GetDecoder(); break; case MyEncodingType.Utf8withBom: if (targetEncoding == Encoding.ASCII) { throw new Exception("当前编码,无法转为ASCII码"); } de = new UTF8Encoding(true).GetDecoder(); rd.ReadBytes(3); break; case MyEncodingType.Utf8withoutBom: if (targetEncoding == Encoding.ASCII) { throw new Exception("当前编码,无法转为ASCII码"); } de = new UTF8Encoding(false).GetDecoder(); break; case MyEncodingType.Utf16Big: if (targetEncoding == Encoding.ASCII) { throw new Exception("当前编码,无法转为ASCII码"); } de = Encoding.BigEndianUnicode.GetDecoder(); rd.ReadBytes(2); break; case MyEncodingType.Utf16Little: if (targetEncoding == Encoding.ASCII) { throw new Exception("当前编码,无法转为ASCII码"); } de = Encoding.Unicode.GetDecoder(); rd.ReadBytes(2); break; default: break; } byte[] readBytes = rd.ReadBytes((int)rd.BaseStream.Length); // 此时 rd.BaseStream.Length 大于剩下的 byte数组数量,但是没关系,只会读取剩下所有的 char[] chars = new char[de.GetCharCount(readBytes, 0, readBytes.Length)]; de.GetChars(readBytes, 0, readBytes.Length, chars, 0); sourceStr = new string(chars); } } //使用新的编码写入文本 using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { using (BinaryWriter writer = new BinaryWriter(fs, targetEncoding)) { // 转换成目标编码的字节数组 byte[] targetBytes = targetEncoding.GetBytes(sourceStr); byte[] preamble = targetEncoding.GetPreamble(); //若有BOM头,则添上Bom头 if (preamble.Length > 0) { byte[] finBytes = new byte[targetBytes.Length + preamble.Length]; Buffer.BlockCopy(preamble, 0, finBytes, 0, preamble.Length); Buffer.BlockCopy(targetBytes, 0, finBytes, preamble.Length, targetBytes.Length); targetBytes = finBytes; } writer.Write(targetBytes); } } }
public bool NegTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest2: ArgumentNullException is not thrown when chars is a null reference "); try { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; chars = null; int charsEncodedCount = utf8.GetChars(bytes, 1, 2, chars, 0); TestLibrary.TestFramework.LogError("102.1", "ArgumentNullException is not thrown when chars is a null reference "); retVal = false; } catch (ArgumentNullException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("102.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public bool NegTest6() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest6: ArgumentOutOfRangeException is not thrown when byteIndex and byteCount do not denote a valid range in chars. "); try { Byte[] bytes; Char[] chars = new Char[] { '\u0023', '\u0025', '\u03a0', '\u03a3' }; UTF8Encoding utf8 = new UTF8Encoding(); int byteCount = utf8.GetByteCount(chars, 1, 2); bytes = new Byte[byteCount]; int charsEncodedCount = utf8.GetChars(bytes, 2, 2, chars, 1); TestLibrary.TestFramework.LogError("106.1", "ArgumentOutOfRangeException is not thrown when byteIndex and byteCount do not denote a valid range in chars."); retVal = false; } catch (ArgumentOutOfRangeException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("106.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public BitmapFont(Canvas canvas, string path) { this.canvas = canvas; #if UNCOMPRESSED_CONTENT string png = PathOp.Combine(DualityApp.DataDirectory, "Animations", path + ".png"); #else string png = PathOp.Combine(DualityApp.DataDirectory, "Main.dz", "Animations", path + ".png"); #endif string pathFont = png + ".font"; int textureHeight; using (Stream s = FileOp.Open(png, FileAccessMode.Read)) { PixelData pixelData = new Png(s).GetPixelData(); textureHeight = pixelData.Height; ColorRgba[] palette = ContentResolver.Current.Palette.Res.BasePixmap.Res.MainLayer.Data; ColorRgba[] data = pixelData.Data; #if !DISABLE_ASYNC Parallel.ForEach(Partitioner.Create(0, data.Length), range => { for (int i = range.Item1; i < range.Item2; i++) { #else for (int i = 0; i < data.Length; i++) { #endif int colorIdx = data[i].R; data[i] = palette[colorIdx].WithAlpha(palette[colorIdx].A * data[i].A / (255f * 255f)); } #if !DISABLE_ASYNC }); #endif Texture texture = new Texture(new Pixmap(pixelData), TextureSizeMode.NonPowerOfTwo, TextureMagFilter.Linear, TextureMinFilter.Linear); materialPlain = new Material(DrawTechnique.Alpha, texture); materialColor = new Material(ContentResolver.Current.RequestShader("Colorize"), texture); } using (Stream s = FileOp.Open(pathFont, FileAccessMode.Read)) { byte[] internalBuffer = new byte[128]; byte flags = s.ReadUInt8(ref internalBuffer); ushort width = s.ReadUInt16(ref internalBuffer); ushort height = s.ReadUInt16(ref internalBuffer); byte cols = s.ReadUInt8(ref internalBuffer); int rows = textureHeight / height; short spacing = s.ReadInt16(ref internalBuffer); int asciiFirst = s.ReadUInt8(ref internalBuffer); int asciiCount = s.ReadUInt8(ref internalBuffer); s.Read(internalBuffer, 0, asciiCount); int i = 0; for (; i < asciiCount; i++) { asciiChars[i + asciiFirst] = new Rect( (float)(i % cols) / cols, (float)(i / cols) / rows, internalBuffer[i], height); } UTF8Encoding enc = new UTF8Encoding(false, true); int unicodeCharCount = asciiCount + s.ReadInt32(ref internalBuffer); for (; i < unicodeCharCount; i++) { s.Read(internalBuffer, 0, 1); int remainingBytes = ((internalBuffer[0] & 240) == 240) ? 3 : ( ((internalBuffer[0] & 224) == 224) ? 2 : ( ((internalBuffer[0] & 192) == 192) ? 1 : -1 )); if (remainingBytes == -1) { throw new InvalidDataException("Char \"" + (char)internalBuffer[0] + "\" is not UTF-8"); } s.Read(internalBuffer, 1, remainingBytes); char c = enc.GetChars(internalBuffer, 0, remainingBytes + 1)[0]; byte charWidth = s.ReadUInt8(ref internalBuffer); unicodeChars[c] = new Rect( (float)(i % cols) / cols, (float)(i / cols) / rows, charWidth, height); } this.charHeight = height; this.baseSpacing = spacing; } }
public bool TryReadChars(char[] chars, int offset, int count, out int actual) { DiagnosticUtility.DebugAssert(offset + count <= chars.Length, string.Format("offset '{0}' + count '{1}' MUST BE <= chars.Length '{2}'", offset, count, chars.Length)); if (_type == ValueHandleType.Unicode) { return(TryReadUnicodeChars(chars, offset, count, out actual)); } if (_type != ValueHandleType.UTF8) { actual = 0; return(false); } int charOffset = offset; int charCount = count; byte[] bytes = _bufferReader.Buffer; int byteOffset = _offset; int byteCount = _length; bool insufficientSpaceInCharsArray = false; var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); while (true) { while (charCount > 0 && byteCount > 0) { // fast path for codepoints U+0000 - U+007F byte b = bytes[byteOffset]; if (b >= 0x80) { break; } chars[charOffset] = (char)b; byteOffset++; byteCount--; charOffset++; charCount--; } if (charCount == 0 || byteCount == 0 || insufficientSpaceInCharsArray) { break; } int actualByteCount; int actualCharCount; try { // If we're asking for more than are possibly available, or more than are truly available then we can return the entire thing if (charCount >= encoding.GetMaxCharCount(byteCount) || charCount >= encoding.GetCharCount(bytes, byteOffset, byteCount)) { actualCharCount = encoding.GetChars(bytes, byteOffset, byteCount, chars, charOffset); actualByteCount = byteCount; } else { Decoder decoder = encoding.GetDecoder(); // Since x bytes can never generate more than x characters this is a safe estimate as to what will fit actualByteCount = Math.Min(charCount, byteCount); // We use a decoder so we don't error if we fall across a character boundary actualCharCount = decoder.GetChars(bytes, byteOffset, actualByteCount, chars, charOffset); // We might have gotten zero characters though if < 4 bytes were requested because // codepoints from U+0000 - U+FFFF can be up to 3 bytes in UTF-8, and represented as ONE char // codepoints from U+10000 - U+10FFFF (last Unicode codepoint representable in UTF-8) are represented by up to 4 bytes in UTF-8 // and represented as TWO chars (high+low surrogate) // (e.g. 1 char requested, 1 char in the buffer represented in 3 bytes) while (actualCharCount == 0) { // Note the by the time we arrive here, if actualByteCount == 3, the next decoder.GetChars() call will read the 4th byte // if we don't bail out since the while loop will advance actualByteCount only after reading the byte. if (actualByteCount >= 3 && charCount < 2) { // If we reach here, it means that we're: // - trying to decode more than 3 bytes and, // - there is only one char left of charCount where we're stuffing decoded characters. // In this case, we need to back off since decoding > 3 bytes in UTF-8 means that we will get 2 16-bit chars // (a high surrogate and a low surrogate) - the Decoder will attempt to provide both at once // and an ArgumentException will be thrown complaining that there's not enough space in the output char array. // actualByteCount = 0 when the while loop is broken out of; decoder goes out of scope so its state no longer matters insufficientSpaceInCharsArray = true; break; } else { DiagnosticUtility.DebugAssert(byteOffset + actualByteCount < bytes.Length, string.Format("byteOffset {0} + actualByteCount {1} MUST BE < bytes.Length {2}", byteOffset, actualByteCount, bytes.Length)); // Request a few more bytes to get at least one character actualCharCount = decoder.GetChars(bytes, byteOffset + actualByteCount, 1, chars, charOffset); actualByteCount++; } } // Now that we actually retrieved some characters, figure out how many bytes it actually was actualByteCount = encoding.GetByteCount(chars, charOffset, actualCharCount); } } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateEncodingException(bytes, byteOffset, byteCount, exception)); } // Advance byteOffset += actualByteCount; byteCount -= actualByteCount; charOffset += actualCharCount; charCount -= actualCharCount; } _offset = byteOffset; _length = byteCount; actual = (count - charCount); return(true); }
public bool TryReadChars(char[] chars, int offset, int count, out int actual) { if (this.type == ValueHandleType.Unicode) { return(this.TryReadUnicodeChars(chars, offset, count, out actual)); } if (this.type != ValueHandleType.UTF8) { actual = 0; return(false); } int index = offset; int num2 = count; byte[] bytes = this.bufferReader.Buffer; int num3 = this.offset; int length = this.length; Label_006C: while ((num2 > 0) && (length > 0)) { byte num5 = bytes[num3]; if (num5 >= 0x80) { break; } chars[index] = (char)num5; num3++; length--; index++; num2--; } if ((num2 != 0) && (length != 0)) { int num6; int num7; UTF8Encoding encoding = new UTF8Encoding(false, true); try { if ((num2 >= encoding.GetMaxCharCount(length)) || (num2 >= encoding.GetCharCount(bytes, num3, length))) { num7 = encoding.GetChars(bytes, num3, length, chars, index); num6 = length; } else { System.Text.Decoder decoder = encoding.GetDecoder(); num6 = Math.Min(num2, length); num7 = decoder.GetChars(bytes, num3, num6, chars, index); while (num7 == 0) { num7 = decoder.GetChars(bytes, num3 + num6, 1, chars, index); num6++; } num6 = encoding.GetByteCount(chars, index, num7); } } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateEncodingException(bytes, num3, length, exception)); } num3 += num6; length -= num6; index += num7; num2 -= num7; goto Label_006C; } this.offset = num3; this.length = length; actual = count - num2; return(true); }
public static PasswordGenerationOptions DeriveOptionsFromPassword(ProtectedString psPassword) { PasswordGenerationOptions opt = new PasswordGenerationOptions(); Debug.Assert(psPassword != null); if (psPassword == null) { return(opt); } opt.CharSpaces.Clear(); UTF8Encoding utf8 = new UTF8Encoding(); byte[] pbUTF8 = psPassword.ReadUTF8(); char[] vChars = utf8.GetChars(pbUTF8); opt.PasswordLength = (uint)vChars.Length; foreach (char ch in vChars) { if ((ch >= 'A') && (ch <= 'Z')) { opt.CharSpaces.UpperCase = true; } else if ((ch >= 'a') && (ch <= 'z')) { opt.CharSpaces.LowerCase = true; } else if ((ch >= '0') && (ch <= '9')) { opt.CharSpaces.Numeric = true; } else if (ch == '_') { opt.CharSpaces.Underline = true; } else if (ch == '-') { opt.CharSpaces.Minus = true; } else if (ch == ' ') { opt.CharSpaces.Space = true; } else if ((@"!#$%&'*+,./:;=?@^").IndexOf(ch) >= 0) { opt.CharSpaces.Special = true; } else if (ch == '\"') { opt.CharSpaces.Special = true; } else if (ch == '\\') { opt.CharSpaces.Special = true; } else if ((@"()[]{}<>").IndexOf(ch) >= 0) { opt.CharSpaces.Brackets = true; } else if (ch >= '~') { opt.CharSpaces.HighANSI = true; } } Array.Clear(vChars, 0, vChars.Length); Array.Clear(pbUTF8, 0, pbUTF8.Length); return(opt); }
private int SendTextSMS(string ClientSerialNo, string SMSKey, string Recepient, string strSenderEmail, string strSenderName, string txtMsg, int canReply) { Object[] obj_params = { ClientSerialNo, SMSKey, Recepient, strSenderEmail, strSenderName, txtMsg, canReply }; WebHeaderCollection req_headers = new WebHeaderCollection(); string req_text = String.Format(RC_req_fmt, obj_params); byte [] req_bytes = new UTF8Encoding().GetBytes(req_text); req_text = null; Uri _req_uri = new Uri(req_uri); HttpWebRequest web_req = (HttpWebRequest)WebRequest.Create(_req_uri); req_headers.Add("SOAPAction", SoapAction); web_req.Headers = req_headers; web_req.ContentType = "text/xml"; web_req.KeepAlive = false; web_req.UserAgent = "Virtuoso SOAP sample"; web_req.Method = "POST"; web_req.ContentLength = req_bytes.Length; Stream req_str = web_req.GetRequestStream(); req_str.Write(req_bytes, 0, req_bytes.Length); req_str.Flush(); req_bytes = null; HttpWebResponse resp = (HttpWebResponse)web_req.GetResponse(); Encoding enc = new UTF8Encoding(); XmlDocument doc = new XmlDocument(); long clen = resp.ContentLength; Stream resp_stream = resp.GetResponseStream(); String content; if (clen != -1) { byte [] bytes = new byte[clen]; long read = 0; while (read < clen) { read += resp_stream.Read(bytes, (int)read, (int)(clen - read)); } UTF8Encoding enc1 = new UTF8Encoding(false, true); content = new String(enc1.GetChars(bytes)); } else if (typeof(NetworkStream).IsInstanceOfType(resp_stream)) { NetworkStream nstr = resp_stream as NetworkStream; byte [] bytes = new byte[10000]; long read = 0; while (nstr.DataAvailable) { read += nstr.Read(bytes, (int)read, (int)(bytes.Length - read)); } UTF8Encoding enc1 = new UTF8Encoding(false, true); content = new String(enc1.GetChars(bytes, 0, (int)read)); } else { resp_stream.Close(); req_str.Close(); throw new Exception("don't know how to read the stream"); } resp_stream.Close(); req_str.Close(); doc.LoadXml(content); XmlNode xml_env = doc.DocumentElement; XmlNode xml_body = xml_env["Body", xml_env.NamespaceURI]; XmlNode xml_fault = xml_body["Fault", xml_env.NamespaceURI]; if (xml_fault != null) { throw new Exception(xml_fault.InnerText); } XmlNode xml_resp = xml_body[SoapResp_name, SoapResp_uri]; XmlNode result = xml_resp[SoapResp_val]; return(Int32.Parse(result.InnerXml)); }
/// <summary> /// Create an mtom message from body parts list /// </summary> /// <param name="bodyParts">Body Parts collection used to create the Mtom message</param> /// <returns>byte array containing an mtom message</returns> public byte[] CreateMessage(WsMtomBodyParts bodyParts) { if (bodyParts == null) { throw new ArgumentNullException("Mtom body parts collection must not be null."); } if (bodyParts.Count == 0) { throw new ArgumentException("Mtom bodyParts must at least contain a soap message body part."); } // Initialize the internal buffer, position and length m_buffer = new byte[m_growSize]; m_curPos = 0; m_length = 0; // If mtom body parts boundary is null generate one if (bodyParts.Boundary == null) { m_bodyParts.Boundary = Guid.NewGuid().ToString() + '-' + Guid.NewGuid().ToString().Substring(0, 33); } else { m_bodyParts.Boundary = bodyParts.Boundary; } // If start is null set to the first body part content id. if (bodyParts.Start == null) { m_bodyParts.Start = bodyParts[0].ContentID; } else { m_bodyParts.Start = bodyParts.Start; } WriteLine("--" + bodyParts.Boundary); for (int i = 0; i < bodyParts.Count; ++i) { WriteLine("Content-Type: " + bodyParts[i].ContentType); WriteLine("Content-Transfer-Encoding: " + bodyParts[i].ContentTransferEncoding); WriteLine("Content-ID: " + bodyParts[i].ContentID); WriteLine(""); string temp = new string(m_utf8Encoding.GetChars(bodyParts[i].Content.ToArray())); WriteBytes(bodyParts[i].Content.ToArray()); WriteLine(""); if (i == bodyParts.Count - 1) { WriteString("--" + bodyParts.Boundary + "--"); } else { WriteLine("--" + bodyParts.Boundary); } } // Create new buffer at the exact length byte[] tempBuff = new byte[m_length]; Array.Copy(m_buffer, tempBuff, m_length); return(tempBuff); }
public IEnumerable <ContentFormat> Identify(IByteContainer container) { var toRead = (int)Math.Min(container.Length, 1024); byte[] bytes = container.Read(0, toRead); if (bytes.Length >= 4 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0xfe && bytes[3] == 0xff) { // UTF-32, big-endian var decoder = new UTF32Encoding(true, false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 4, bytes.Length - 4, chars, 0); if (decoded > (int)(((toRead - 4) / 4) * 0.9)) { yield return(CreateTextFormat("utf-32/big-endian", decoder, bytes, (decoded * 4) + 4)); yield break; } } else if (bytes.Length >= 4 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0xfe && bytes[3] == 0xff) { // UTF-32, little-endian var decoder = new UTF32Encoding(false, false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 4, bytes.Length - 4, chars, 0); if (decoded > (int)(((toRead - 4) / 4) * 0.9)) { yield return(CreateTextFormat("utf-32/little-endian", decoder, bytes, (decoded * 4) + 4)); yield break; } } else if (bytes.Length >= 2 && bytes[0] == 0xfe && bytes[1] == 0xff) { // UTF-16, big-endian var decoder = new UnicodeEncoding(true, false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 2, bytes.Length - 2, chars, 0); if (decoded > (int)(((toRead - 2) / 2) * 0.9)) { yield return(CreateTextFormat("utf-16/big-endian", decoder, bytes, (decoded * 2) + 2)); yield break; } } else if (bytes.Length >= 2 && bytes[0] == 0xff && bytes[1] == 0xfe) { // UTF-16, little-endian var decoder = new UnicodeEncoding(false, false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 2, bytes.Length - 2, chars, 0); if (decoded > (int)(((toRead - 2) / 2) * 0.9)) { yield return(CreateTextFormat("utf-16/little-endian", decoder, bytes, (decoded * 2) + 2)); yield break; } } else if (bytes.Length >= 3 && bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf) { // UTF-8 var decoder = new UTF8Encoding(false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 3, bytes.Length - 3, chars, 0); if (decoded > (int)(((toRead - 3) / 2) * 0.8)) { yield return(CreateTextFormat("utf-8", decoder, bytes, (decoded * 2) + 2)); yield break; } } var encoders = new Dictionary <string, Encoding> { { "utf-8", new UTF8Encoding(true, true) }, { "cp1252", Encoding.GetEncoding("Windows-1252") }, { "iso-8859-1", Encoding.GetEncoding("iso-8859-1") }, { "utf-16/big-endian", new UnicodeEncoding(true, false, true) }, { "utf-16/little-endian", new UnicodeEncoding(false, false, true) }, { "utf-32/big-endian", new UTF32Encoding(true, false, true) }, { "utf-32/little-endian", new UTF32Encoding(false, false, true) }, }; foreach (var encoder in encoders) { byte[] bytes2; try { string s = encoder.Value.GetString(bytes, 0, bytes.Length); bytes2 = encoder.Value.GetBytes(s.Substring(0, (int)(s.Length * 0.9))); } catch { continue; } if (bytes2.Length == 0) { continue; } if (bytes.Any(b => b == 0)) { continue; } bool isMatch = true; for (int index = 0; index < bytes2.Length; index++) { if (bytes[index] != bytes2[index]) { isMatch = false; break; } } if (isMatch) { yield return(CreateTextFormat(encoder.Key, encoder.Value, bytes, bytes2.Length)); yield break; } } }
public static int GetChars(ReadOnlySpan <byte> bytes, Span <char> chars) => Encoder.GetChars(bytes, chars);
public void OnMessage(SerialMessenger sender, byte[] message, int length) { var str = new string(Encoder.GetChars(message, 0, length)); Debug.Print(str); }