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)

來源:
UTF7Encoding.cs
來源:
UTF7Encoding.cs
來源:
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 所表示的位置上寫入的實際位元組數目。

屬性

例外狀況

charsnull (Nothing)。

-或-

bytesnull (Nothing)。

charCountbyteCount 小於零。

byteCount 小於結果位元組數。

發生 Fallback (如需完整說明,請參閱 .NET 中的字元編碼)。

-和-

EncoderFallback 設定為 EncoderExceptionFallback

備註

若要計算儲存所產生位元組所需的 GetBytes 確切陣列大小,應用程式會使用 GetByteCount 。 若要計算陣列大小上限,應用程式應該使用 GetMaxByteCount 。 方法 GetByteCount 通常允許配置較少的記憶體,而 GetMaxByteCount 方法通常會執行得更快。

要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分成較小的區塊,應用程式應該分別使用 Decoder 方法 Encoder 或 方法所提供的 GetDecoderGetEncoder

注意

UTF7Encoding 不提供錯誤偵測。 無效字元會編碼為修改的基底 64 個字元。 基於安全性考慮,建議您使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

另請參閱

適用於

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

來源:
UTF7Encoding.cs
來源:
UTF7Encoding.cs
來源:
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 的實際位元組數。

例外狀況

charsnull (Nothing)。

-或-

bytesnull (Nothing)。

charIndexcharCountbyteIndex 小於零。

-或-

charIndexcharCount 不代表 chars 中有效的範圍。

-或-

byteIndexbytes 中不是有效的索引。

bytes 到陣列結尾處,byteIndex 沒有足夠的容量容納結果位元組。

發生 Fallback (如需完整說明,請參閱 .NET 中的字元編碼)。

-和-

EncoderFallback 設定為 EncoderExceptionFallback

範例

下列程式碼範例示範如何使用 GetBytes 方法,從 String 編碼字元範圍,並將編碼的位元組儲存在位元組陣列中的元素範圍內。

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'};
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   int byteCount = utf7->GetByteCount( chars, 1, 2 );
   bytes = gcnew array<Byte>(byteCount);
   int bytesEncodedCount = utf7->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 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 方法通常會執行得更快。

要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分成較小的區塊,應用程式應該分別使用 Decoder 方法 Encoder 或 方法所提供的 GetDecoderGetEncoder

注意

UTF7Encoding 不提供錯誤偵測。 無效字元會編碼為修改的基底 64 個字元。 基於安全性考慮,建議您使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

另請參閱

適用於

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

來源:
UTF7Encoding.cs
來源:
UTF7Encoding.cs
來源:
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 的實際位元組數。

屬性

例外狀況

snull (Nothing)。

-或-

bytesnull (Nothing)。

charIndexcharCountbyteIndex 小於零。

-或-

charIndexcharCount 不代表 chars 中有效的範圍。

-或-

byteIndexbytes 中不是有效的索引。

bytes 到陣列結尾處,byteIndex 沒有足夠的容量容納結果位元組。

發生 Fallback (如需完整說明,請參閱 .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'};
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   int byteCount = utf7->GetByteCount( chars, 1, 2 );
   bytes = gcnew array<Byte>(byteCount);
   int bytesEncodedCount = utf7->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 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 方法通常會執行得更快。

要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分成較小的區塊,應用程式應該分別使用 Decoder 方法 Encoder 或 方法所提供的 GetDecoderGetEncoder

注意

UTF7Encoding 不提供錯誤偵測。 無效字元會編碼為修改的基底 64 個字元。 基於安全性考慮,建議您使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

另請參閱

適用於