UTF8Encoding.GetByteCount 方法

定义

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

重载

GetByteCount(ReadOnlySpan<Char>)

计算对指定字符范围进行编码所产生的字节数。

GetByteCount(String)

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

GetByteCount(Char*, Int32)

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

GetByteCount(Char[], Int32, Int32)

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

GetByteCount(ReadOnlySpan<Char>)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

计算对指定字符范围进行编码所产生的字节数。

public:
 override int GetByteCount(ReadOnlySpan<char> chars);
public override int GetByteCount (ReadOnlySpan<char> chars);
override this.GetByteCount : ReadOnlySpan<char> -> int
Public Overrides Function GetByteCount (chars As ReadOnlySpan(Of Char)) As Integer

参数

chars
ReadOnlySpan<Char>

包含要编码的字符集的范围。

返回

对指定字符范围进行编码所产生的字节数。

例外

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

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

-和-

EncoderFallback 设置为 EncoderExceptionFallback

注解

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

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

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

适用于

GetByteCount(String)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

计算对指定 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)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.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.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)>]
[<System.Security.SecurityCritical>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
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

参数

chars
Char*

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

count
Int32

要编码的字符的数目。

返回

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

属性

例外

charsnull

count 小于零。

- 或 -

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

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

发生了回退(如需完整说明,请参阅 .NET 中的字符编码)。

-和-

EncoderFallback 设置为 EncoderExceptionFallback

注解

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

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

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

另请参阅

适用于

GetByteCount(Char[], Int32, Int32)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.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 的值进行比较,该值指示编码拉丁文小写字符所需的最大字节数。

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 的值中。

另请参阅

适用于