UTF32Encoding.GetByteCount 方法

定义

计算对一组字符进行编码时产生的字节数。

重载

GetByteCount(String)

计算对指定 String 中的字符进行编码时所产生的字节数。

GetByteCount(Char*, Int32)

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

GetByteCount(Char[], Int32, Int32)

计算对指定字符数组中的一组字符进行编码时产生的字节数。

GetByteCount(String)

Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs

计算对指定 String 中的字符进行编码时所产生的字节数。

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

参数

s
String

包含要编码的字符集的 String

返回

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

例外

snull

得到的字节数大于可作为整数返回的最大数量。

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

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

-和-

EncoderFallback 设置为 EncoderExceptionFallback

示例

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

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        String chars = "UTF-32 Encoding Example";
        Encoding enc = Encoding.UTF32;

        Console.WriteLine("Bytes needed to encode '{0}':", chars);
        Console.WriteLine("   Maximum:         {0}",
                          enc.GetMaxByteCount(chars.Length));
        Console.WriteLine("   Actual:          {0}",
                          enc.GetByteCount(chars));
        Console.WriteLine("   Actual with BOM: {0}",
                          enc.GetByteCount(chars) + enc.GetPreamble().Length);
    }
}
// The example displays the following output:
//       Bytes needed to encode 'UTF-32 Encoding Example':
//          Maximum:         96
//          Actual:          92
//          Actual with BOM: 96
Imports System.Text

Module Example
    Public Sub Main()
        Dim chars As String = "UTF-32 Encoding Example"
        Dim enc As Encoding = Encoding.UTF32

        Console.WriteLine("Bytes needed to encode '{0}':", chars)
        Console.WriteLine("   Maximum:         {0}",
                          enc.GetMaxByteCount(chars.Length))
        Console.WriteLine("   Actual:          {0}",
                          enc.GetByteCount(chars))
        Console.WriteLine("   Actual with BOM: {0}",
                          enc.GetByteCount(chars) + enc.GetPreamble().Length)
    End Sub
End Module
' The example displays the following output:
'       Bytes needed to encode 'UTF-32 Encoding Example':
'          Maximum:         96
'          Actual:          92
'          Actual with BOM: 96

注解

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

使用错误检测时,无效序列会导致此方法引发 ArgumentException。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。

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

另请参阅

适用于

GetByteCount(Char*, Int32)

Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs

重要

此 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)]
public override int GetByteCount (char* chars, int count);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
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 的字符编码

-和-

EncoderFallback 设置为 EncoderExceptionFallback

注解

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

使用错误检测时,无效序列会导致此方法引发 ArgumentException。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。

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

另请参阅

适用于

GetByteCount(Char[], Int32, Int32)

Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs
Source:
UTF32Encoding.cs

计算对指定字符数组中的一组字符进行编码时产生的字节数。

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 的值进行比较,该值指示编码拉丁文小写字符所需的最大字节数。 以下示例使用希腊文和西里尔文字符的组合填充数组,并调用 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 enc = Encoding.UTF32;

      // 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}",
                        enc.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual:          {0,5:N0}",
                        enc.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual with BOM: {0,5:N0}",
                        enc.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1) +
                                          enc.GetPreamble().Length);
   }
}
// The example displays the following output:
//       Bytes needed for lowercase Latin characters:
//          Maximum:           108
//          Actual:            104
//          Actual with BOM:   108
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 enc As Encoding = Encoding.UTF32
      
      ' 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}",
                          enc.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual:          {0,5:N0}",
                          enc.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual with BOM: {0,5:N0}",
                          enc.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1) +
                                            enc.GetPreamble().Length)
   End Sub
End Module
' The example displays the following output:
'       Bytes needed for lowercase Latin characters:
'          Maximum:           108
'          Actual:            104
'          Actual with BOM:   108

注解

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

使用错误检测时,无效序列会导致此方法引发 ArgumentException。 如果不进行错误检测,将忽略无效序列,并且不会引发异常。

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

另请参阅

适用于