当前位置: 首页>>代码示例>>C#>>正文


C# UTF8Encoding.GetCharCount方法代码示例

本文整理汇总了C#中System.Text.UTF8Encoding.GetCharCount方法的典型用法代码示例。如果您正苦于以下问题:C# UTF8Encoding.GetCharCount方法的具体用法?C# UTF8Encoding.GetCharCount怎么用?C# UTF8Encoding.GetCharCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.UTF8Encoding的用法示例。


在下文中一共展示了UTF8Encoding.GetCharCount方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PosTest2

 public void PosTest2()
 {
     Byte[] bytes = new Byte[] { };
     UTF8Encoding utf8 = new UTF8Encoding();
     int charCount = utf8.GetCharCount(bytes, 0, 0);
     Assert.Equal(0, charCount);
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:7,代码来源:UTF8EncodingGetCharCount.cs

示例2: NegTest1

 public void NegTest1()
 {
     Byte[] bytes = null;
     UTF8Encoding utf8 = new UTF8Encoding();
     Assert.Throws<ArgumentNullException>(() =>
    {
        int charCount = utf8.GetCharCount(bytes, 2, 8);
    });
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:9,代码来源:UTF8EncodingGetCharCount.cs

示例3: ConvertBytesToUTF8

		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);
		}
开发者ID:nguyenhuuhuy,项目名称:mygeneration,代码行数:11,代码来源:CollectionSerializer.cs

示例4: Base64Decode

        /// <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);
        }
开发者ID:extnet,项目名称:Utilities,代码行数:16,代码来源:StringUtils.cs

示例5: PosTest1

        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);
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:11,代码来源:UTF8EncodingGetCharCount.cs

示例6: NegTest3

 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);
     });
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:12,代码来源:UTF8EncodingGetCharCount.cs

示例7: DecodeFromBase64

        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);
        }
开发者ID:aozora,项目名称:arashi,代码行数:12,代码来源:Base64Extensions.cs

示例8: FromBase64

        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);
            }
        }
开发者ID:anukat2015,项目名称:sones,代码行数:19,代码来源:StringExtensions.cs

示例9: TryReadChars

        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)
                {
//.........这里部分代码省略.........
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:101,代码来源:ValueHandle.cs

示例10: Dispose

 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)
//.........这里部分代码省略.........
开发者ID:JBTech,项目名称:Dot.Utility,代码行数:101,代码来源:DiagramViewLicenseProvider.cs

示例11: TryReadChars

        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;
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:88,代码来源:ValueHandle.cs

示例12: Base64_Decode

 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);
 }
开发者ID:angelog,项目名称:Coral,代码行数:8,代码来源:GameConsole.cs

示例13: ShinjiToQji

 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);
 }
开发者ID:marionette-of-u,项目名称:RevText,代码行数:48,代码来源:Program.cs

示例14: TryReadChars

 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;
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:70,代码来源:ValueHandle.cs


注:本文中的System.Text.UTF8Encoding.GetCharCount方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。