UTF7Encoding.GetBytes 方法

定义

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

重载

名称 说明
GetBytes(Char*, Int32, Byte*, Int32)

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

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

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

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

将指定 String 字符集编码为指定的字节数组。

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

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.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位置写入的实际字节数。

属性

例外

charsnullNothing)。

-或-

bytesnullNothing)。

charCountbyteCount 小于零。

byteCount 小于生成的字节数。

发生回退(有关更完整的说明,请参阅 .NET>Character Encoding)。

-以及-

EncoderFallback 设置为 EncoderExceptionFallback

注解

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

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

注释

UTF7Encoding 不提供错误检测。 无效字符编码为已修改的 base 64 字符。 出于安全原因,建议 UTF8Encoding使用应用程序, UnicodeEncodingUTF32Encoding 启用错误检测。

另请参阅

适用于

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

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.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到的实际字节数。

例外

charsnullNothing)。

-或-

bytesnullNothing)。

charIndexcharCountbyteIndex小于零。

-或-

charIndexcharCount不表示有效范围。chars

-或-

byteIndex不是有效的索引。bytes

bytes 数组的末尾没有足够的容量 byteIndex 来容纳生成的字节。

发生回退(有关更完整的说明,请参阅 .NET>Character Encoding)。

-以及-

EncoderFallback 设置为 EncoderExceptionFallback

示例

下面的代码示例演示如何使用 GetBytes 该方法对字节 String 数组中的元素范围中的字符范围进行编码,并将编码的字节存储在字节数组中的一系列元素中。

using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        Byte[] bytes;
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };
        
        UTF7Encoding utf7 = new UTF7Encoding();
        
        int byteCount = utf7.GetByteCount(chars, 1, 2);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf7.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 UTF7EncodingExample
    
    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 utf7 As New UTF7Encoding()
        
        Dim byteCount As Integer = utf7.GetByteCount(chars, 1, 2)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf7.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 该方法通常执行速度更快。

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

注释

UTF7Encoding 不提供错误检测。 无效字符编码为已修改的 base 64 字符。 出于安全原因,建议 UTF8Encoding使用应用程序, UnicodeEncodingUTF32Encoding 启用错误检测。

另请参阅

适用于

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

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.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);
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex);
override this.GetBytes : string * int * int * byte[] * int -> int
[<System.Runtime.InteropServices.ComVisible(false)>]
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到的实际字节数。

属性

例外

snullNothing)。

-或-

bytesnullNothing)。

charIndexcharCountbyteIndex小于零。

-或-

charIndexcharCount不表示有效范围。s

-或-

byteIndex不是有效的索引。bytes

bytes 数组的末尾没有足够的容量 byteIndex 来容纳生成的字节。

发生回退(有关更完整的说明,请参阅 .NET>Character Encoding)。

-以及-

EncoderFallback 设置为 EncoderExceptionFallback

示例

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

using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        Byte[] bytes;
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };
        
        UTF7Encoding utf7 = new UTF7Encoding();
        
        int byteCount = utf7.GetByteCount(chars, 1, 2);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf7.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 UTF7EncodingExample
    
    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 utf7 As New UTF7Encoding()
        
        Dim byteCount As Integer = utf7.GetByteCount(chars, 1, 2)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf7.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 该方法通常执行速度更快。

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

注释

UTF7Encoding 不提供错误检测。 无效字符编码为已修改的 base 64 字符。 出于安全原因,建议 UTF8Encoding使用应用程序, UnicodeEncodingUTF32Encoding 启用错误检测。

另请参阅

适用于