Encoder.GetBytes 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,将一组字符编码为字节序列。
重载
| 名称 | 说明 |
|---|---|
| GetBytes(ReadOnlySpan<Char>, Span<Byte>, Boolean) |
在派生类中重写时,将输入字符范围中的一组字符和内部缓冲区中的任何字符编码为存储在输入字节范围中的字节序列。 参数指示是否在转换后清除编码器的内部状态。 |
| GetBytes(Char*, Int32, Byte*, Int32, Boolean) |
在派生类中重写时,将一组从指定字符指针开始的字符和内部缓冲区中的任何字符编码为从指定字节指针开始存储的字节序列。 参数指示是否在转换后清除编码器的内部状态。 |
| GetBytes(Char[], Int32, Int32, Byte[], Int32, Boolean) |
在派生类中重写时,将指定字符数组中的一组字符和内部缓冲区中的任何字符编码为指定的字节数组。 参数指示是否在转换后清除编码器的内部状态。 |
注解
请记住,对象 Encoder 在调用 GetBytes之间保存状态。 当应用程序使用数据流完成时,它应将参数true设置为flush上一次调用GetBytes,以确保刷新状态信息并正确终止编码的字节。 使用此设置,编码器会忽略数据块末尾的无效字节,例如不匹配的代理项或不完整的组合序列,并清除内部缓冲区。
若要计算存储生成的字符所需的确切缓冲区大小 GetBytes ,应用程序应使用 GetByteCount。
如果使用GetBytes立即调用GetByteCount数据块,以便计算中包括上一个块中的任何尾随字符。
如果应用程序要转换输入流的许多段,请考虑使用该方法 Convert 。 GetBytes 如果输出缓冲区不够大,但 Convert 会尽可能填充空间并返回写入的字符读取和字节,将引发异常。 另请参阅主题 Encoding.GetBytes 以获取更多注释。
GetBytes(ReadOnlySpan<Char>, Span<Byte>, Boolean)
在派生类中重写时,将输入字符范围中的一组字符和内部缓冲区中的任何字符编码为存储在输入字节范围中的字节序列。 参数指示是否在转换后清除编码器的内部状态。
public:
virtual int GetBytes(ReadOnlySpan<char> chars, Span<System::Byte> bytes, bool flush);
public virtual int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, bool flush);
abstract member GetBytes : ReadOnlySpan<char> * Span<byte> * bool -> int
override this.GetBytes : ReadOnlySpan<char> * Span<byte> * bool -> int
Public Overridable Function GetBytes (chars As ReadOnlySpan(Of Char), bytes As Span(Of Byte), flush As Boolean) As Integer
参数
- chars
- ReadOnlySpan<Char>
要编码的字符范围。
- flush
- Boolean
true 如果转换后清除编码器的内部状态,则为 ;否则,为 false.
返回
在参数指示 bytes 的位置写入的实际字节数。
适用于
GetBytes(Char*, Int32, Byte*, Int32, Boolean)
重要
此 API 不符合 CLS。
在派生类中重写时,将一组从指定字符指针开始的字符和内部缓冲区中的任何字符编码为从指定字节指针开始存储的字节序列。 参数指示是否在转换后清除编码器的内部状态。
public:
virtual int GetBytes(char* chars, int charCount, System::Byte* bytes, int byteCount, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush);
[System.CLSCompliant(false)]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush);
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int * bool -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Security.SecurityCritical>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int * bool -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int * bool -> int
[<System.CLSCompliant(false)>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int * bool -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int * bool -> int
参数
- chars
- Char*
指向要编码的第一个字符的指针。
- charCount
- Int32
要编码的字符数。
- bytes
- Byte*
指向开始写入生成的字节序列的位置的指针。
- byteCount
- Int32
要写入的最大字节数。
- flush
- Boolean
true 如果转换后清除编码器的内部状态,则为 ;否则,为 false.
返回
在参数指示 bytes 的位置写入的实际字节数。
- 属性
例外
charCount 或 byteCount 小于零。
byteCount 小于生成的字节数。
适用于
GetBytes(Char[], Int32, Int32, Byte[], Int32, Boolean)
在派生类中重写时,将指定字符数组中的一组字符和内部缓冲区中的任何字符编码为指定的字节数组。 参数指示是否在转换后清除编码器的内部状态。
public:
abstract int GetBytes(cli::array <char> ^ chars, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex, bool flush);
public abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush);
abstract member GetBytes : char[] * int * int * byte[] * int * bool -> int
Public MustOverride Function GetBytes (chars As Char(), charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer, flush As Boolean) As Integer
参数
- chars
- Char[]
包含要编码的字符集的字符数组。
- charIndex
- Int32
要编码的第一个字符的索引。
- charCount
- Int32
要编码的字符数。
- bytes
- Byte[]
要包含生成的字节序列的字节数组。
- byteIndex
- Int32
开始写入生成的字节序列的索引。
- flush
- Boolean
true 如果转换后清除编码器的内部状态,则为 ;否则,为 false.
返回
写入 bytes到的实际字节数。
例外
charIndex或charCountbyteIndex小于零。
-或-
charIndex且charCount不表示有效范围。chars
-或-
byteIndex不是有效的索引。bytes
bytes 数组的末尾没有足够的容量 byteIndex 来容纳生成的字节。
示例
以下示例演示如何对字符数组中的元素范围进行编码,并将编码的字节存储在字节数组中的元素范围内。 该方法 GetByteCount 用于确定所需的 GetBytes数组的大小。
using System;
using System.Text;
class EncoderExample {
public static void Main() {
Byte[] bytes;
// 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);
bytes = new Byte[byteCount];
int bytesEncodedCount = uniEncoder.GetBytes(chars, 0, chars.Length, bytes, 0, true);
Console.WriteLine(
"{0} bytes used to encode characters.", bytesEncodedCount
);
Console.Write("Encoded bytes: ");
foreach (Byte b in bytes) {
Console.Write("[{0}]", b);
}
Console.WriteLine();
}
}
/* This code example produces the following output.
8 bytes used to encode characters.
Encoded bytes: [35][0][37][0][160][3][163][3]
*/
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class EncoderExample
Public Shared Sub Main()
Dim bytes() As Byte
' 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)
bytes = New Byte(byteCount - 1) {}
Dim bytesEncodedCount As Integer = _
uniEncoder.GetBytes(chars, 0, chars.Length, bytes, 0, True)
Console.WriteLine( _
"{0} bytes used to encode characters.", _
bytesEncodedCount _
)
Console.Write("Encoded bytes: ")
Dim b As Byte
For Each b In bytes
Console.Write("[{0}]", b)
Next b
Console.WriteLine()
End Sub
End Class
'This code example produces the following output.
'8 bytes used to encode characters.
'Encoded bytes: [35][0][37][0][160][3][163][3]
'