public void PosTest2() { Byte[] bytes = new Byte[] { }; UTF8Encoding utf8 = new UTF8Encoding(); int charCount = utf8.GetCharCount(bytes, 0, 0); Assert.Equal(0, charCount); }
public void NegTest1() { Byte[] bytes = null; UTF8Encoding utf8 = new UTF8Encoding(); Assert.Throws<ArgumentNullException>(() => { int charCount = utf8.GetCharCount(bytes, 2, 8); }); }
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); }
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); }
/// <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 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); }); }
public static string DecodeFromBase64(this string input) { Decoder decoder = new UTF8Encoding().GetDecoder(); byte[] bytes = Convert.FromBase64String(input); int charCount = decoder.GetCharCount(bytes, 0, bytes.Length); char[] chars = new char[charCount]; decoder.GetChars(bytes, 0, bytes.Length, chars, 0); return new String(chars); }
static public int GetCharCount__A_Byte(IntPtr l) { try { System.Text.UTF8Encoding self = (System.Text.UTF8Encoding)checkSelf(l); System.Byte[] a1; checkArray(l, 2, out a1); var ret = self.GetCharCount(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
public static String FromBase64(this String myBase64String) { try { var _UTF8Decoder = new UTF8Encoding().GetDecoder(); var _Bytes = Convert.FromBase64String(myBase64String); var _DecodedChars = new Char[_UTF8Decoder.GetCharCount(_Bytes, 0, _Bytes.Length)]; _UTF8Decoder.GetChars(_Bytes, 0, _Bytes.Length, _DecodedChars, 0); return new String(_DecodedChars); } catch (Exception e) { throw new Exception("Error in base64Decode" + e.Message, e); } }
private string ReadAsciiString(BinaryReader reader) { byte b = reader.ReadByte(); byte[] bt = new byte[b]; for (byte z = 0; z < b; z++) { byte c = reader.ReadByte(); bt[z] = c; } var encoding = Encoding.GetEncoding(1251); var coding = new System.Text.UTF8Encoding(false); var utf = Encoding.Convert(encoding, coding, bt); // Convert the new byte[] into a char[] and then into a string. char[] utfChars = new char[coding.GetCharCount(utf, 0, utf.Length)]; coding.GetChars(utf, 0, utf.Length, utfChars, 0); return(new string(utfChars)); }
private string Base64_Decode(string input) { Decoder utf8Decode = new UTF8Encoding().GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(input); 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); }
public bool TryReadChars(char[] chars, int offset, int count, out int actual) { Fx.Assert(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 = this.offset; int byteCount = this.length; bool insufficientSpaceInCharsArray = false; 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; 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 < 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 { Fx.Assert(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; } this.offset = byteOffset; this.length = byteCount; actual = (count - charCount); return true; }
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 }
//| <include path='docs/doc[@for="UTF8Encoding.UTF8Decoder.GetCharCount"]/*' /> public override int GetCharCount(byte[] bytes, int index, int count) { return(encoding.GetCharCount(bytes, index, count, this)); }
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 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; }
static string ShinjiToQji(string str) { if (!qJi) { return str; } int length = str.Length; byte[] unicodeByteStr = new byte[(length + 3) * 5]; int unicodeByteStrCounter = 0; char[] charArray = new char[length]; for (int i = 0; i < str.Length; ++i) { UInt16 charCode = (UInt16)str[i]; int n = Search(charCode, neworderNew); if (n < 0) { unicodeByteStr[unicodeByteStrCounter + 0] = (byte)(charCode & 0xFF); unicodeByteStr[unicodeByteStrCounter + 1] = (byte)((charCode >> 8) & 0xFF); unicodeByteStrCounter += 2; } else { UInt16 a = neworderOld[n]; if (translateCode) { string code = "&#" + a.ToString() + ";"; for (int j = 0; j < code.Length; ++j) { UInt16 c = (UInt16)code[j]; unicodeByteStr[unicodeByteStrCounter + 0] = (byte)(c & 0xFF); unicodeByteStr[unicodeByteStrCounter + 1] = (byte)((c >> 8) & 0xFF); unicodeByteStrCounter += 2; } } else { unicodeByteStr[unicodeByteStrCounter + 0] = (byte)(a & 0xFF); unicodeByteStr[unicodeByteStrCounter + 1] = (byte)((a >> 8) & 0xFF); unicodeByteStrCounter += 2; } } } System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(); byte[] utf8Bytes = System.Text.Encoding.Convert(new System.Text.UnicodeEncoding(), utf8, unicodeByteStr, 0, unicodeByteStrCounter); char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes)]; utf8Chars = utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length); return new string(utf8Chars); }
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; }