UTF8Encoding.GetBytes 方法

定义

将一组字符编码为一个字节序列。

重载

GetBytes(String)

将指定的 String 对象中的字符编码为一个字节序列。

GetBytes(ReadOnlySpan<Char>, Span<Byte>)

将指定的字符范围编码为指定的字节范围。

GetBytes(Char*, Int32, Byte*, Int32)

将从指定的字符指针开始的一组字符编码为一个字节序列,并从指定的字节指针开始存储该字节序列。

GetBytes(Char[], Int32, Int32, Byte[], Int32)

将指定字符数组中的一组字符编码到指定的字节数组中。

GetBytes(String, Int32, Int32, Byte[], Int32)

将指定 String 中的一组字符编码到指定的字节数组中。

GetBytes(String)

将指定的 String 对象中的字符编码为一个字节序列。

public:
 override cli::array <System::Byte> ^ GetBytes(System::String ^ s);
public override byte[] GetBytes (string s);
override this.GetBytes : string -> byte[]
Public Overrides Function GetBytes (s As String) As Byte()

参数

s
String

要编码的字符串。

返回

Byte[]

包含 s 参数指定的字符串中编码字符的字节数组。

适用于

GetBytes(ReadOnlySpan<Char>, Span<Byte>)

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

将指定的字符范围编码为指定的字节范围。

public:
 override int GetBytes(ReadOnlySpan<char> chars, Span<System::Byte> bytes);
public override int GetBytes (ReadOnlySpan<char> chars, Span<byte> bytes);
override this.GetBytes : ReadOnlySpan<char> * Span<byte> -> int
Public Overrides Function GetBytes (chars As ReadOnlySpan(Of Char), bytes As Span(Of Byte)) As Integer

参数

chars
ReadOnlySpan<Char>

要编码的字符范围。

bytes
Span<Byte>

要包含生成的字节集的范围。

返回

写入 bytes 的实际字节数。

注解

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

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

要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者,如果数据量太大,需要将其划分为较小的块,请分别使用 Decoder 方法或 Encoder 方法返回的 GetDecoderGetEncoder

若要确保在编码字节保存为文件或流时正确解码,可以使用前言作为编码字节流前缀。 开发人员负责在字节流的开头插入前言 (例如要写入文件的一系列字节的开头,) 。 方法 GetBytes 不会在编码字节序列的开头添加前言。

适用于

GetBytes(Char*, Int32, Byte*, Int32)

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

重要

此 API 不符合 CLS。

将从指定的字符指针开始的一组字符编码为一个字节序列,并从指定的字节指针开始存储该字节序列。

public:
 override int GetBytes(char* chars, int charCount, System::Byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int

参数

chars
Char*

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

charCount
Int32

要编码的字符的数目。

bytes
Byte*

一个指针,指向开始写入所产生的字节序列的位置。

byteCount
Int32

最多写入的字节数。

返回

在由 bytes 指示的位置处写入的实际字节数。

属性

例外

charsnull

- 或 -

bytesnull

charCountbyteCount 小于零。

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

- 或 -

byteCount 少于所产生的字节数。

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

-和-

EncoderFallback 设置为 EncoderExceptionFallback

注解

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

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

要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者,如果数据量太大,需要将其划分为较小的块,请分别使用 Decoder 方法或 Encoder 方法返回的 GetDecoderGetEncoder

若要确保在编码字节保存为文件或流时正确解码,可以使用前言作为编码字节流前缀。 开发人员负责在字节流的开头插入前言 (例如要写入文件的一系列字节的开头,) 。 方法 GetBytes 不会在编码字节序列的开头添加前言。

另请参阅

适用于

GetBytes(Char[], Int32, Int32, Byte[], Int32)

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

将指定字符数组中的一组字符编码到指定的字节数组中。

public:
 override int GetBytes(cli::array <char> ^ chars, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);
public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex);
override this.GetBytes : char[] * int * int * byte[] * int -> int
Public Overrides Function GetBytes (chars As Char(), charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer) As Integer

参数

chars
Char[]

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

charIndex
Int32

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

charCount
Int32

要编码的字符的数目。

bytes
Byte[]

要包含所产生的字节序列的字节数组。

byteIndex
Int32

要开始写入所产生的字节序列的索引位置。

返回

写入 bytes 的实际字节数。

例外

charsnull

- 或 -

bytesnull

charIndexcharCountbyteIndex 小于零。

- 或 -

charIndexcharCount 不表示 chars中的有效范围。

- 或 -

byteIndex 不是 bytes 中的有效索引。

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

- 或 -

bytes 中从 byteIndex 到数组结尾没有足够的容量来容纳所产生的字节。

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

-和-

EncoderFallback 设置为 EncoderExceptionFallback

示例

下面的示例使用 GetBytes 方法对字符串中的字符范围进行编码,并将编码的字节存储在字节数组的元素范围中。

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Byte>^bytes;
   String^ chars = "UTF8 Encoding Example";
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   int byteCount = utf8->GetByteCount( chars->ToCharArray(), 0, 13 );
   bytes = gcnew array<Byte>(byteCount);
   int bytesEncodedCount = utf8->GetBytes( chars, 0, 13, bytes, 0 );
   Console::WriteLine( "{0} bytes used to encode string.", bytesEncodedCount );
   Console::Write( "Encoded bytes: " );
   IEnumerator^ myEnum = bytes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Byte b = safe_cast<Byte>(myEnum->Current);
      Console::Write( "[{0}]", b );
   }

   Console::WriteLine();
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Byte[] bytes;
        String chars = "UTF8 Encoding Example";
        
        UTF8Encoding utf8 = new UTF8Encoding();
        
        int byteCount = utf8.GetByteCount(chars.ToCharArray(), 0, 13);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf8.GetBytes(chars, 0, 13, bytes, 0);
        
        Console.WriteLine(
            "{0} bytes used to encode string.", bytesEncodedCount
        );

        Console.Write("Encoded bytes: ");
        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
    }
}
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte
        Dim chars As String = "UTF8 Encoding Example"
        
        Dim utf8 As New UTF8Encoding()
        
        Dim byteCount As Integer = utf8.GetByteCount(chars.ToCharArray(), 0, 13)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf8.GetBytes(chars, 0, 13, bytes, 0)
        
        Console.WriteLine("{0} bytes used to encode string.", 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

注解

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

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

要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,请分别使用 Decoder 方法或 Encoder 方法提供的 GetDecoderGetEncoder

若要确保在编码字节保存为文件或流时正确解码,可以使用前言作为编码字节流前缀。 开发人员负责在字节流的开头插入前言 (例如要写入文件的一系列字节的开头,) 。 方法 GetBytes 不会在编码字节序列的开头添加前言。

另请参阅

适用于

GetBytes(String, Int32, Int32, Byte[], Int32)

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

将指定 String 中的一组字符编码到指定的字节数组中。

public:
 override int GetBytes(System::String ^ s, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);
public override int GetBytes (string s, int charIndex, int charCount, byte[] bytes, int byteIndex);
override this.GetBytes : string * int * int * byte[] * int -> int
Public Overrides Function GetBytes (s As String, charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer) As Integer

参数

s
String

包含要编码的字符集的 String

charIndex
Int32

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

charCount
Int32

要编码的字符的数目。

bytes
Byte[]

要包含所产生的字节序列的字节数组。

byteIndex
Int32

要开始写入所产生的字节序列的索引位置。

返回

写入 bytes 的实际字节数。

例外

snull

- 或 -

bytesnull

charIndexcharCountbyteIndex 小于零。

- 或 -

charIndexcharCount 不表示 chars中的有效范围。

- 或 -

byteIndex 不是 bytes 中的有效索引。

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

- 或 -

bytes 中从 byteIndex 到数组结尾没有足够的容量来容纳所产生的字节。

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

-和-

EncoderFallback 设置为 EncoderExceptionFallback

示例

以下示例使用 GetBytes 方法对 Unicode 字符数组中的元素范围进行编码,并将编码的字节存储在字节数组中的元素范围中。

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Byte>^bytes;
   
   // Unicode characters.
   
   // Pi
   // Sigma
   array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   int byteCount = utf8->GetByteCount( chars, 1, 2 );
   bytes = gcnew array<Byte>(byteCount);
   int bytesEncodedCount = utf8->GetBytes( chars, 1, 2, bytes, 0 );
   Console::WriteLine( "{0} bytes used to encode characters.", bytesEncodedCount );
   Console::Write( "Encoded bytes: " );
   IEnumerator^ myEnum = bytes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Byte b = safe_cast<Byte>(myEnum->Current);
      Console::Write( "[{0}]", b );
   }

   Console::WriteLine();
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Byte[] bytes;
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };
        
        UTF8Encoding utf8 = new UTF8Encoding();
        
        int byteCount = utf8.GetByteCount(chars, 1, 2);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf8.GetBytes(chars, 1, 2, bytes, 0);
        
        Console.WriteLine(
            "{0} bytes used to encode characters.", bytesEncodedCount
        );

        Console.Write("Encoded bytes: ");
        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
    }
}
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UTF8EncodingExample
    
    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 utf8 As New UTF8Encoding()
        
        Dim byteCount As Integer = utf8.GetByteCount(chars, 1, 2)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf8.GetBytes(chars, 1, 2, bytes, 0)
        
        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

注解

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

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

要转换的数据(例如从流读取的数据)可能仅在顺序块中可用。 在这种情况下,或者如果数据量太大,需要将其划分为较小的块,请分别使用 Decoder 方法或 Encoder 方法提供的 GetDecoderGetEncoder

若要确保在编码字节保存为文件或流时正确解码,可以使用前言作为编码字节流前缀。 开发人员负责在字节流的开头插入前言 (例如要写入文件的一系列字节的开头,) 。 方法 GetBytes 不会在编码字节序列的开头添加前言。

另请参阅

适用于