Encoder.GetByteCount 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,计算通过编码一组字符生成的字节数。
重载
| 名称 | 说明 |
|---|---|
| GetByteCount(ReadOnlySpan<Char>, Boolean) |
在派生类中重写时,计算通过编码“chars”范围中的一组字符生成的字节数。 参数指示是否在计算后清除编码器的内部状态。 |
| GetByteCount(Char*, Int32, Boolean) |
在派生类中重写时,计算通过编码一组字符(从指定字符指针开始)生成的字节数。 参数指示是否在计算后清除编码器的内部状态。 |
| GetByteCount(Char[], Int32, Int32, Boolean) |
在派生类中重写时,计算通过编码指定字符数组中的一组字符生成的字节数。 参数指示是否在计算后清除编码器的内部状态。 |
注解
此方法不会影响编码器的状态。
若要计算存储生成的字节所需的确切数组大小 GetBytes ,应用程序应使用 GetByteCount。
如果使用GetByteCount立即调用GetBytes数据块,以便计算中包括上一个块中的任何尾随字符。
GetByteCount(ReadOnlySpan<Char>, Boolean)
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
在派生类中重写时,计算通过编码“chars”范围中的一组字符生成的字节数。 参数指示是否在计算后清除编码器的内部状态。
public:
virtual int GetByteCount(ReadOnlySpan<char> chars, bool flush);
public virtual int GetByteCount(ReadOnlySpan<char> chars, bool flush);
abstract member GetByteCount : ReadOnlySpan<char> * bool -> int
override this.GetByteCount : ReadOnlySpan<char> * bool -> int
Public Overridable Function GetByteCount (chars As ReadOnlySpan(Of Char), flush As Boolean) As Integer
参数
- chars
- ReadOnlySpan<Char>
要编码的字符范围。
- flush
- Boolean
true 模拟在计算后清除编码器的内部状态;否则,为 false.
返回
通过对指定字符和内部缓冲区中的任何字符进行编码生成的字节数。
适用于
GetByteCount(Char*, Int32, Boolean)
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
重要
此 API 不符合 CLS。
在派生类中重写时,计算通过编码一组字符(从指定字符指针开始)生成的字节数。 参数指示是否在计算后清除编码器的内部状态。
public:
virtual int GetByteCount(char* chars, int count, bool flush);
[System.CLSCompliant(false)]
public virtual int GetByteCount(char* chars, int count, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetByteCount(char* chars, int count, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual int GetByteCount(char* chars, int count, bool flush);
[<System.CLSCompliant(false)>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Security.SecurityCritical>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
参数
- chars
- Char*
指向要编码的第一个字符的指针。
- count
- Int32
要编码的字符数。
- flush
- Boolean
true 模拟在计算后清除编码器的内部状态;否则,为 false.
返回
通过对指定字符和内部缓冲区中的任何字符进行编码生成的字节数。
- 属性
例外
chars 在 Visual Basic .NET 中为 null (Nothing)。
count 小于零。
另请参阅
适用于
GetByteCount(Char[], Int32, Int32, Boolean)
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
- Source:
- Encoder.cs
在派生类中重写时,计算通过编码指定字符数组中的一组字符生成的字节数。 参数指示是否在计算后清除编码器的内部状态。
public:
abstract int GetByteCount(cli::array <char> ^ chars, int index, int count, bool flush);
public abstract int GetByteCount(char[] chars, int index, int count, bool flush);
abstract member GetByteCount : char[] * int * int * bool -> int
Public MustOverride Function GetByteCount (chars As Char(), index As Integer, count As Integer, flush As Boolean) As Integer
参数
- chars
- Char[]
包含要编码的字符集的字符数组。
- index
- Int32
要编码的第一个字符的索引。
- count
- Int32
要编码的字符数。
- flush
- Boolean
true 模拟在计算后清除编码器的内部状态;否则,为 false.
返回
通过对指定字符和内部缓冲区中的任何字符进行编码生成的字节数。
例外
chars 是 null。
示例
下面的代码示例演示如何使用 GetByteCount 该方法返回使用 Unicode Encoder对字符数组进行编码所需的字节数。
using System;
using System.Text;
class EncoderExample {
public static void Main() {
// Unicode characters.
Char[] chars = new Char[] {
'\u0023', // #
'\u0025', // %
'\u03a0', // Pi
'\u03a3' // Sigma
};
Encoder uniEncoder = Encoding.Unicode.GetEncoder();
int byteCount = uniEncoder.GetByteCount(chars, 0, chars.Length, true);
Console.WriteLine(
"{0} bytes needed to encode characters.", byteCount
);
}
}
/* This example produces the following output.
8 bytes needed to encode characters.
*/
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class EncoderExample
Public Shared Sub Main()
' Unicode characters.
' ChrW(35) = #
' ChrW(37) = %
' ChrW(928) = Pi
' ChrW(931) = Sigma
Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
Dim uniEncoder As Encoder = Encoding.Unicode.GetEncoder()
Dim byteCount As Integer = _
uniEncoder.GetByteCount(chars, 0, chars.Length, True)
Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
End Sub
End Class
'
'This example produces the following output.
'
'8 bytes needed to encode characters.
'