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


C# Encoding.GetByteCount方法代码示例

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


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

示例1: GetByteCount

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

示例2: GetByteCount

        private static unsafe void GetByteCount(Encoding encoding, string chars, int index, int count, int expected)
        {
            char[] charArray = chars.ToCharArray();
            if (index == 0 && count == chars.Length)
            {
                // Use GetByteCount(string) or GetByteCount(char[])
                Assert.Equal(expected, encoding.GetByteCount(chars));
                Assert.Equal(expected, encoding.GetByteCount(charArray));
            }
            // Use GetByteCount(char[], int, int)
            Assert.Equal(expected, encoding.GetByteCount(charArray, index, count));

            fixed (char* pChars = chars)
            {
                // Use GetByteCount(char*, int)
                Assert.Equal(expected, encoding.GetByteCount(pChars + index, count));
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:18,代码来源:EncodingTestHelpers.cs

示例3: GetByteCount_Invalid

        public static void GetByteCount_Invalid(Encoding encoding)
        {
            // Chars is null
            Assert.Throws<ArgumentNullException>(encoding is ASCIIEncoding ? "chars" : "s", () => encoding.GetByteCount((string)null));
            Assert.Throws<ArgumentNullException>("chars", () => encoding.GetByteCount((char[])null));
            Assert.Throws<ArgumentNullException>("chars", () => encoding.GetByteCount(null, 0, 0));

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

            // Index + count > chars.Length
            Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 0, 4));
            Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 1, 3));
            Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 2, 2));
            Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 3, 1));
            Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 4, 0));
        }
开发者ID:eerhardt,项目名称:corefx,代码行数:18,代码来源:NegativeEncodingTests.cs

示例4: 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

示例5: GetSBytesForEncoding

 private static sbyte[] GetSBytesForEncoding(Encoding encoding, string s)
 {
     var sbytes = new sbyte[encoding.GetByteCount(s)];
     encoding.GetBytes(s, 0, s.Length, (byte[]) (object) sbytes, 0);
     return sbytes;
 }
开发者ID:shayhatsor,项目名称:curator,代码行数:6,代码来源:StringHelperClass.cs

示例6: 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

示例7: WriteString

    /// <summary>
    /// Writes the specified string to the underlying stream.
    /// </summary>
    /// <param name="value">The string value.</param>
    /// <param name="length">The fixed length.</param>
    /// <param name="encoding">The character encoding.</param>
    public static void WriteString(this BinaryWriter writer, string value, int length, Encoding encoding, char paddingCharacter = '\0')
    {
        byte[] values = encoding.GetBytes(value);
        Array.Resize<byte>(ref values, length);

        for (int i = encoding.GetByteCount(value); i < length; i++) {
            values[i] = (byte)paddingCharacter;
        }

        writer.Write(values);
    }
开发者ID:osROSE,项目名称:UnityRose,代码行数:17,代码来源:BinaryWriter.cs

示例8: WriteShortString

 /// <summary>
 /// Writes the specified string pre-fixed with the string length as a 16-bit integer to the underlying stream.
 /// </summary>
 /// <param name="value">The string value.</param>
 /// <param name="encoding">The character encoding.</param>
 public static void WriteShortString(this BinaryWriter writer, string value, Encoding encoding)
 {
     writer.Write((short)encoding.GetByteCount(value));
     writer.WriteString(value, encoding);
 }
开发者ID:osROSE,项目名称:UnityRose,代码行数:10,代码来源:BinaryWriter.cs

示例9: Encode_Invalid

        public static unsafe void Encode_Invalid(Encoding encoding, string chars, int index, int count)
        {
            Assert.Equal(EncoderFallback.ExceptionFallback, encoding.EncoderFallback);

            char[] charsArray = chars.ToCharArray();
            byte[] bytes = new byte[encoding.GetMaxByteCount(count)];

            if (index == 0 && count == chars.Length)
            {
                Assert.Throws<EncoderFallbackException>(() => encoding.GetByteCount(chars));
                Assert.Throws<EncoderFallbackException>(() => encoding.GetByteCount(charsArray));

                Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(chars));
                Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(charsArray));
            }
            Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(charsArray, index, count));

            Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(chars, index, count, bytes, 0));
            Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(charsArray, index, count, bytes, 0));

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

                Assert.Throws<EncoderFallbackException>(() => encoding.GetByteCount(pCharsLocal + index, count));
                Assert.Throws<EncoderFallbackException>(() => encoding.GetBytes(pCharsLocal + index, count, pBytesLocal, bytes.Length));
            }
        }
开发者ID:SGuyGe,项目名称:corefx,代码行数:30,代码来源:NegativeEncodingTests.cs

示例10: NegativeTestChars2

 public bool NegativeTestChars2(Encoding enc, char[] str, int index, int count, Type excType, string id)
 {
     bool result = true;
     TestFramework.BeginScenario(id + ": Getting byte count with encoding " + enc.WebName);
     try
     {
         int output = enc.GetByteCount(str, index, count);
         result = false;
         TestFramework.LogError("009", "Error in " + id + ", Expected exception not thrown. Actual byte count " + 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:l1183479157,项目名称:coreclr,代码行数:20,代码来源:encodinggetbytecount.cs

示例11: PositiveTestChars

 public bool PositiveTestChars(Encoding enc, char[] chars, int expected, string id)
 {
     bool result = true;
     TestFramework.BeginScenario(id + ": Getting byte count for " + new string(chars) + " with encoding " + enc.WebName);
     try
     {
         int output = enc.GetByteCount(chars);
         if (output != expected)
         {
             result = false;
             TestFramework.LogError("003", "Error in " + id + ", unexpected comparison result. Actual byte count " + output + ", Expected: " + expected);
         }
     }
     catch (Exception exc)
     {
         result = false;
         TestFramework.LogError("004", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
     }
     return result;
 }
开发者ID:l1183479157,项目名称:coreclr,代码行数:20,代码来源:encodinggetbytecount.cs


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