UTF8Encoding.GetByteCount 方法

定义

计算编码一组字符生成的字节数。

重载

名称 说明
GetByteCount(String)

计算通过对指定 String字符进行编码生成的字节数。

GetByteCount(Char*, Int32)

计算从指定字符指针开始对一组字符进行编码生成的字节数。

GetByteCount(Char[], Int32, Int32)

计算通过对指定字符数组中的一组字符进行编码而生成的字节数。

GetByteCount(String)

计算通过对指定 String字符进行编码生成的字节数。

public:
 override int GetByteCount(System::String ^ chars);
public override int GetByteCount(string chars);
override this.GetByteCount : string -> int
Public Overrides Function GetByteCount (chars As String) As Integer

参数

chars
String

String包含要编码的字符集。

返回

通过对指定字符进行编码生成的字节数。

例外

charsnull

生成的字节数大于可作为整数返回的最大数。

启用错误检测,并 chars 包含无效的字符序列。

发生回退(有关详细信息,请参阅 .NET 中的字符编码

-以及-

EncoderFallback 设置为 EncoderExceptionFallback

示例

下面的示例调用和GetMaxByteCountGetByteCount(String)方法来计算对字符串进行编码所需的最大字节数和实际字节数。 它还显示使用字节顺序标记存储字节流所需的实际字节数。

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        String chars = "UTF8 Encoding Example";
        Encoding utf8 = Encoding.UTF8;

        Console.WriteLine("Bytes needed to encode '{0}':", chars);
        Console.WriteLine("   Maximum:         {0}",
                          utf8.GetMaxByteCount(chars.Length));
        Console.WriteLine("   Actual:          {0}",
                          utf8.GetByteCount(chars));
        Console.WriteLine("   Actual with BOM: {0}",
                          utf8.GetByteCount(chars) + utf8.GetPreamble().Length);
    }
}
// The example displays the following output:
//       Bytes needed to encode 'UTF8 Encoding Example':
//          Maximum:         66
//          Actual:          21
//          Actual with BOM: 24
Imports System.Text

Module Example
    Public Sub Main()
        Dim chars As String = "UTF8 Encoding Example"
        Dim utf8 As Encoding = Encoding.UTF8

        Console.WriteLine("Bytes needed to encode '{0}':", chars)
        Console.WriteLine("   Maximum:         {0}",
                          utf8.GetMaxByteCount(chars.Length))
        Console.WriteLine("   Actual:          {0}",
                          utf8.GetByteCount(chars))
        Console.WriteLine("   Actual with BOM: {0}",
                          utf8.GetByteCount(chars) + utf8.GetPreamble().Length)
    End Sub
End Module
' The example displays the following output:
'       Bytes needed to encode 'UTF8 Encoding Example':
'          Maximum:         66
'          Actual:          21
'          Actual with BOM: 24

注解

若要计算存储生成的字节所需的 GetBytes 确切数组大小,请调用 GetByteCount 该方法。 若要计算最大数组大小,请调用 GetMaxByteCount 该方法。 该方法 GetByteCount 通常分配较少的内存,而 GetMaxByteCount 该方法通常执行速度更快。

在错误检测中,无效序列会导致此方法引发 ArgumentException 异常。 如果没有错误检测,将忽略无效序列,并且不会引发异常。

为了确保编码的字节在保存为文件或流时正确解码,可以将编码的字节流作为前言前缀。 在字节流开头(如要写入文件的一系列字节的开头)插入谓词是开发人员的责任,并且前言中的字节数不会反映在方法返回 GetByteCount 的值中。

另请参阅

适用于

GetByteCount(Char*, Int32)

重要

此 API 不符合 CLS。

计算从指定字符指针开始对一组字符进行编码生成的字节数。

public:
 override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
public override int GetByteCount(char* chars, int count);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
override this.GetByteCount : nativeptr<char> * int -> int

参数

chars
Char*

指向要编码的第一个字符的指针。

count
Int32

要编码的字符数。

返回

通过对指定字符进行编码生成的字节数。

属性

例外

charsnull

count 小于零。

-或-

生成的字节数大于可作为整数返回的最大数。

启用错误检测,并 chars 包含无效的字符序列。

发生回退(有关完整说明,请参阅 .NET>Character Encoding)。

-以及-

EncoderFallback 设置为 EncoderExceptionFallback

注解

若要计算方法存储生成的字节所需的 GetBytes 确切数组大小,请调用该方法 GetByteCount 。 若要计算最大数组大小,请调用 GetMaxByteCount 该方法。 该方法 GetByteCount 通常分配较少的内存,而 GetMaxByteCount 该方法通常执行速度更快。

在错误检测中,无效序列会导致此方法引发 ArgumentException 异常。 如果没有错误检测,将忽略无效序列,并且不会引发异常。

为了确保编码的字节在保存为文件或流时正确解码,可以将编码的字节流作为前言前缀。 在字节流开头(如要写入文件的一系列字节的开头)插入谓词是开发人员的责任,并且前言中的字节数不会反映在方法返回 GetByteCount 的值中。

另请参阅

适用于

GetByteCount(Char[], Int32, Int32)

计算通过对指定字符数组中的一组字符进行编码而生成的字节数。

public:
 override int GetByteCount(cli::array <char> ^ chars, int index, int count);
public override int GetByteCount(char[] chars, int index, int count);
override this.GetByteCount : char[] * int * int -> int
Public Overrides Function GetByteCount (chars As Char(), index As Integer, count As Integer) As Integer

参数

chars
Char[]

包含要编码的字符集的字符数组。

index
Int32

要编码的第一个字符的索引。

count
Int32

要编码的字符数。

返回

通过对指定字符进行编码生成的字节数。

例外

charsnull

indexcount 小于零。

-或-

indexcount不表示有效范围。chars

-或-

生成的字节数大于可作为整数返回的最大数。

启用错误检测,并 chars 包含无效的字符序列。

发生回退(有关详细信息,请参阅 .NET 中的字符编码

-以及-

EncoderFallback 属性设置为 EncoderExceptionFallback

示例

以下示例使用拉丁文大写和小写字符填充数组,并调用 GetByteCount(Char[], Int32, Int32) 该方法来确定对拉丁文小写字符进行编码所需的字节数。 然后,它会显示此信息以及添加字节顺序标记时所需的字节总数。 它将此数字与方法返回 GetMaxByteCount 的值进行比较,该值指示对拉丁文小写字符进行编码所需的最大字节数。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      int uppercaseStart = 0x0041;
      int uppercaseEnd = 0x005a;
      int lowercaseStart = 0x0061;
      int lowercaseEnd = 0x007a;
      // Instantiate a UTF8 encoding object with BOM support.
      Encoding utf8 = new UTF8Encoding(true);

      // Populate array with characters.
      char[] chars = new char[lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 2];
      int index = 0;
      for (int ctr = uppercaseStart; ctr <= uppercaseEnd; ctr++) {
         chars[index] = (char)ctr;
         index++;
      }
      for (int ctr = lowercaseStart; ctr <= lowercaseEnd; ctr++) {
         chars[index] = (char)ctr;
         index++;
      }

      // Display the bytes needed for the lowercase characters.
      Console.WriteLine("Bytes needed for lowercase Latin characters:");
      Console.WriteLine("   Maximum:         {0,5:N0}",
                        utf8.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual:          {0,5:N0}",
                        utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual with BOM: {0,5:N0}",
                        utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1) +
                                          utf8.GetPreamble().Length);
   }
}
// The example displays the following output:
//       Bytes needed for lowercase Latin characters:
//          Maximum:            81
//          Actual:             26
//          Actual with BOM:    29
Imports System.Text

Module Example
   Public Sub Main()
      Dim uppercaseStart As Integer = &h0041
      Dim uppercaseEnd As Integer = &h005a
      Dim lowercaseStart As Integer = &h0061
      Dim lowercaseEnd As Integer = &h007a
      ' Instantiate a UTF8 encoding object with BOM support.
      Dim utf8 As New UTF8Encoding(True)
      
      ' Populate array with characters.
      Dim chars(lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 1) As Char
      Dim index As Integer = 0
      For ctr As Integer = uppercaseStart To uppercaseEnd
         chars(index) = ChrW(ctr)
         index += 1
      Next
      For ctr As Integer = lowercaseStart To lowercaseEnd
         chars(index) = ChrW(ctr)
         index += 1
      Next

      ' Display the bytes needed for the lowercase characters.
        Console.WriteLine("Bytes needed for lowercase Latin characters:")
        Console.WriteLine("   Maximum:         {0,5:N0}",
                          utf8.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual:          {0,5:N0}",
                          utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual with BOM: {0,5:N0}",
                          utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1) +
                                            utf8.GetPreamble().Length)
   End Sub
End Module
' The example displays the following output:
'       Bytes needed for lowercase Latin characters:
'          Maximum:            81
'          Actual:             26
'          Actual with BOM:    29

注解

若要计算存储生成的字节所需的 GetBytes 确切数组大小,请调用 uses GetByteCount 方法。 若要计算最大数组大小,请调用 GetMaxByteCount 该方法。 该方法 GetByteCount 通常分配较少的内存,而 GetMaxByteCount 该方法通常执行速度更快。

在错误检测中,无效序列会导致此方法引发 ArgumentException 异常。 如果没有错误检测,将忽略无效序列,并且不会引发异常。

为了确保编码的字节在保存为文件或流时正确解码,可以将编码的字节流作为前言前缀。 在字节流开头(如要写入文件的一系列字节的开头)插入谓词是开发人员的责任,并且前言中的字节数不会反映在方法返回 GetByteCount 的值中。

另请参阅

适用于