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


C# Encoding.GetCharCount方法代码示例

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


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

示例1: GetCharCount

 public static void GetCharCount(Encoding encoding, byte[] bytes, int index, int count, int expected)
 {
     if (index == 0 && count == bytes.Length)
     {
         // Use GetCharCount(byte[])
         Assert.Equal(expected, encoding.GetCharCount(bytes));
     }
     // Use GetCharCount(byte[], int, int)
     Assert.Equal(expected, encoding.GetCharCount(bytes, index, count));
 }
开发者ID:Dmitry-Me,项目名称:corefx,代码行数:10,代码来源:EncodingTestHelpers.cs

示例2: TestEncoding

 private void TestEncoding(Encoding enc, int byteCount, int maxByteCount, byte[] bytes)
 {
     Assert.Equal(byteCount, enc.GetByteCount(s_myChars));
     Assert.Equal(maxByteCount, enc.GetMaxByteCount(s_myChars.Length));
     Assert.Equal(enc.GetBytes(s_myChars), bytes);
     Assert.Equal(enc.GetCharCount(bytes), s_myChars.Length);
     Assert.Equal(enc.GetChars(bytes), s_myChars);
     Assert.Equal(enc.GetString(bytes, 0, bytes.Length), s_myString);
     Assert.NotEqual(0, enc.GetHashCode());
 }
开发者ID:noahfalk,项目名称:corefx,代码行数:10,代码来源:ConvertUnicodeEncodings.cs

示例3: ValidateConsoleEncoding

    public static unsafe void ValidateConsoleEncoding(Encoding encoding)
    {
        Assert.NotNull(encoding);
        // The primary purpose of ConsoleEncoding is to return an empty preamble.
        Assert.Equal(Array.Empty<byte>(), encoding.GetPreamble());

        // There's not much validation we can do, but we can at least invoke members
        // to ensure they don't throw exceptions as they delegate to the underlying
        // encoding wrapped by ConsoleEncoding.

        Assert.False(string.IsNullOrWhiteSpace(encoding.EncodingName));
        Assert.False(string.IsNullOrWhiteSpace(encoding.WebName));
        Assert.True(encoding.CodePage >= 0);
        bool ignored = encoding.IsSingleByte;

        // And we can validate that the encoding is self-consistent by roundtripping
        // data between chars and bytes.

        string str = "This is the input string.";
        char[] strAsChars = str.ToCharArray();
        byte[] strAsBytes = encoding.GetBytes(str);
        Assert.Equal(strAsBytes.Length, encoding.GetByteCount(str));
        Assert.True(encoding.GetMaxByteCount(str.Length) >= strAsBytes.Length);

        Assert.Equal(str, encoding.GetString(strAsBytes));
        Assert.Equal(str, encoding.GetString(strAsBytes, 0, strAsBytes.Length));
        Assert.Equal(str, new string(encoding.GetChars(strAsBytes)));
        Assert.Equal(str, new string(encoding.GetChars(strAsBytes, 0, strAsBytes.Length)));
        fixed (byte* bytesPtr = strAsBytes)
        {
            char[] outputArr = new char[encoding.GetMaxCharCount(strAsBytes.Length)];

            int len = encoding.GetChars(strAsBytes, 0, strAsBytes.Length, outputArr, 0);
            Assert.Equal(str, new string(outputArr, 0, len));
            Assert.Equal(len, encoding.GetCharCount(strAsBytes));
            Assert.Equal(len, encoding.GetCharCount(strAsBytes, 0, strAsBytes.Length));

            fixed (char* charsPtr = outputArr)
            {
                len = encoding.GetChars(bytesPtr, strAsBytes.Length, charsPtr, outputArr.Length);
                Assert.Equal(str, new string(charsPtr, 0, len));
                Assert.Equal(len, encoding.GetCharCount(bytesPtr, strAsBytes.Length));
            }

            Assert.Equal(str, encoding.GetString(bytesPtr, strAsBytes.Length));
        }

        Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars));
        Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars, 0, strAsChars.Length));
        Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars));
        Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars, 0, strAsChars.Length));
        fixed (char* charsPtr = strAsChars)
        {
            Assert.Equal(strAsBytes.Length, encoding.GetByteCount(charsPtr, strAsChars.Length));

            byte[] outputArr = new byte[encoding.GetMaxByteCount(strAsChars.Length)];
            Assert.Equal(strAsBytes.Length, encoding.GetBytes(strAsChars, 0, strAsChars.Length, outputArr, 0));
            fixed (byte* bytesPtr = outputArr)
            {
                Assert.Equal(strAsBytes.Length, encoding.GetBytes(charsPtr, strAsChars.Length, bytesPtr, outputArr.Length));
            }
            Assert.Equal(strAsBytes.Length, encoding.GetBytes(str, 0, str.Length, outputArr, 0));
        }
    }
开发者ID:ESgarbi,项目名称:corefx,代码行数:64,代码来源:ReadAndWrite.cs

示例4: ConvertTo

    public static string ConvertTo(Encoding srcEnc, Encoding dstEnc, string srcStr)
    {
        // Convert the string into a byte[].
        byte[] srcBytes = srcEnc.GetBytes(srcStr);

        // Perform the conversion from one encoding to the other.
        byte[] dstBytes = Encoding.Convert(srcEnc, dstEnc, srcBytes);

        // Convert the new byte[] into a char[] and then into a string.
        // This is a slightly different approach to converting to illustrate
        // the use of GetCharCount/GetChars.
        char[] dstChars = new char[dstEnc.GetCharCount(dstBytes, 0, dstBytes.Length)];
        dstEnc.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);
        return new string(dstChars);
    }
开发者ID:MasatomoSegawa,项目名称:SuiteMassShipura,代码行数:15,代码来源:SsUtilities.cs

示例5: Decode_Invalid

        public static unsafe void Decode_Invalid(Encoding encoding, byte[] bytes, int index, int count)
        {
            Assert.Equal(DecoderFallback.ExceptionFallback, encoding.DecoderFallback);

            char[] chars = new char[encoding.GetMaxCharCount(count)];

            if (index == 0 && count == bytes.Length)
            {
                Assert.Throws<DecoderFallbackException>(() => encoding.GetCharCount(bytes));

                Assert.Throws<DecoderFallbackException>(() => encoding.GetChars(bytes));
                Assert.Throws<DecoderFallbackException>(() => encoding.GetString(bytes));
            }

            Assert.Throws<DecoderFallbackException>(() => encoding.GetCharCount(bytes, index, count));

            Assert.Throws<DecoderFallbackException>(() => encoding.GetChars(bytes, index, count));
            Assert.Throws<DecoderFallbackException>(() => encoding.GetString(bytes, index, count));

            Assert.Throws<DecoderFallbackException>(() => encoding.GetChars(bytes, index, count, chars, 0));

            fixed (byte* pBytes = bytes)
            fixed (char* pChars = chars)
            {
                byte* pBytesLocal = pBytes;
                char* pCharsLocal = pChars;

                Assert.Throws<DecoderFallbackException>(() => encoding.GetCharCount(pBytesLocal + index, count));

                Assert.Throws<DecoderFallbackException>(() => encoding.GetChars(pBytesLocal + index, count, pCharsLocal, chars.Length));
                Assert.Throws<DecoderFallbackException>(() => encoding.GetString(pBytesLocal + index, count));
            }
        }
开发者ID:SGuyGe,项目名称:corefx,代码行数:33,代码来源:NegativeEncodingTests.cs

示例6: GetCharCount_Invalid

        public static unsafe void GetCharCount_Invalid(Encoding encoding)
        {
            // Bytes is null
            Assert.Throws<ArgumentNullException>("bytes", () => encoding.GetCharCount(null));
            Assert.Throws<ArgumentNullException>("bytes", () => encoding.GetCharCount(null, 0, 0));

            // Index or count < 0
            Assert.Throws<ArgumentOutOfRangeException>("index", () => encoding.GetCharCount(new byte[4], -1, 0));
            Assert.Throws<ArgumentOutOfRangeException>("count", () => encoding.GetCharCount(new byte[4], 0, -1));

            // Index + count > bytes.Length
            Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetCharCount(new byte[4], 5, 0));
            Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetCharCount(new byte[4], 4, 1));
            Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetCharCount(new byte[4], 3, 2));
            Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetCharCount(new byte[4], 2, 3));
            Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetCharCount(new byte[4], 1, 4));
            Assert.Throws<ArgumentOutOfRangeException>("bytes", () => encoding.GetCharCount(new byte[4], 0, 5));

            byte[] bytes = new byte[4];
            fixed (byte* pBytes = bytes)
            {
                byte* pBytesLocal = pBytes;
                Assert.Throws<ArgumentNullException>("bytes", () => encoding.GetCharCount(null, 0));
                Assert.Throws<ArgumentOutOfRangeException>("count", () => encoding.GetCharCount(pBytesLocal, -1));
            }
        }
开发者ID:SGuyGe,项目名称:corefx,代码行数:26,代码来源:NegativeEncodingTests.cs

示例7: NegativeTestChars2

 public bool NegativeTestChars2(Encoding enc, byte[] bytes, int index, int count, Type excType, string id)
 {
     bool result = true;
     TestFramework.BeginScenario(id + ": Getting bytes with encoding " + enc.WebName);
     try
     {
         int output = enc.GetCharCount(bytes, index, count);
         result = false;
         TestFramework.LogError("009", "Error in " + id + ", Expected exception not thrown. Actual chars " + output + ", Expected exception type: " + excType.ToString());
     }
     catch (Exception exc)
     {
         if (exc.GetType() != excType)
         {
             result = false;
             TestFramework.LogError("010", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
         }
     }
     return result;
 }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:20,代码来源:encodinggetcharcount.cs

示例8: PositiveTestString

 public bool PositiveTestString(Encoding enc, int expected, byte[] bytes, string id)
 {
     bool result = true;
     TestFramework.BeginScenario(id + ": Getting bytes for " + Utilities.ByteArrayToString(bytes) + " with encoding " + enc.WebName);
     try
     {
         int output = enc.GetCharCount(bytes);
         if (expected != output)
         {
             result = false;
             TestFramework.LogError("001", "Error in " + id + ", unexpected comparison result. Actual chars " + output + ", Expected: " + expected);
         }
     }
     catch (Exception exc)
     {
         result = false;
         TestFramework.LogError("002", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
     }
     return result;
 }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:20,代码来源:encodinggetcharcount.cs

示例9: GetCharCount

        private static unsafe void GetCharCount(Encoding encoding, byte[] bytes, int index, int count, int expected)
        {
            if (index == 0 && count == bytes.Length)
            {
                // Use GetCharCount(byte[])
                Assert.Equal(expected, encoding.GetCharCount(bytes));
            }
            // Use GetCharCount(byte[], int, int)
            Assert.Equal(expected, encoding.GetCharCount(bytes, index, count));

            // Use GetCharCount(byte*, int) - only works for non-null/non-empty byte*
            if (expected > 0)
            {
                fixed (byte* pBytes = bytes)
                {
                    Assert.Equal(expected, encoding.GetCharCount(pBytes + index, count));
                }
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:19,代码来源:EncodingTestHelpers.cs


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