UTF7Encoding.GetBytes 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將一組字元編碼成一串位元組。
多載
| 名稱 | Description |
|---|---|
| GetBytes(Char*, Int32, Byte*, Int32) |
將一組從指定字元指標開始的字元編碼成一串位元組,並從指定位元組指標開始儲存。 |
| GetBytes(Char[], Int32, Int32, Byte[], Int32) |
將指定字元陣列中的一組字元編碼到指定的位元組陣列中。 |
| GetBytes(String, Int32, Int32, Byte[], Int32) |
將指定的 String 一組字元編碼到指定的位元組陣列中。 |
GetBytes(Char*, Int32, Byte*, Int32)
重要
此 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)]
[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)]
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)>]
[<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
[<System.CLSCompliant(false)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
參數
- chars
- Char*
一個指向第一個要編碼字元的指標。
- charCount
- Int32
需要編碼的字元數。
- bytes
- Byte*
一個指向開始寫入產生的位元組序列位置的指標。
- byteCount
- Int32
最大可寫入位元組數。
傳回
在該 bytes位置實際寫入的位元組數。
- 屬性
例外狀況
charCount 或 byteCount 小於零。
byteCount 小於所得位元組數。
隨後出現了備用機制(詳見字元編碼.NET的詳細說明)。
-及-
備註
為了計算儲存所得位元組所需的 GetBytes 精確陣列大小,應用程式使用 GetByteCount。 要計算最大陣列大小,應用程式應使用 GetMaxByteCount。 此 GetByteCount 方法通常允許分配較少的記憶體,而執行 GetMaxByteCount 速度通常較快。
待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量龐大到需要分割成較小區塊時,應用程式應分別使用DecoderEncoder方法或GetEncoder方法所提供的GetDecoder。
Note
UTF7Encoding 無法提供錯誤偵測。 無效字元則以修改過的 64 進位字元編碼。 出於安全考量,建議您的應用程式使用 UTF8Encoding、 , UnicodeEncoding或 UTF32Encoding 啟用錯誤偵測功能。
另請參閱
適用於
GetBytes(Char[], Int32, Int32, Byte[], Int32)
將指定字元陣列中的一組字元編碼到指定的位元組陣列中。
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的實際位元組數。
例外狀況
charIndex 或 charCount 或 byteIndex 小於零。
-或-
charIndex 且 charCount 不表示在 中的 chars有效範圍。
-或-
byteIndex 在 中 不是有效的指標 bytes。
bytes 從陣列到末端的容量 byteIndex 不足以容納產生的位元組。
隨後出現了備用機制(詳見字元編碼.NET的詳細說明)。
-及-
範例
以下程式碼範例示範如何使用此 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方法或GetEncoder方法所提供的GetDecoder。
Note
UTF7Encoding 無法提供錯誤偵測。 無效字元則以修改過的 64 進位字元編碼。 出於安全考量,建議您的應用程式使用 UTF8Encoding、 , UnicodeEncoding或 UTF32Encoding 啟用錯誤偵測功能。
另請參閱
適用於
GetBytes(String, Int32, Int32, Byte[], Int32)
將指定的 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
參數
- charIndex
- Int32
第一個要編碼字元的索引。
- charCount
- Int32
需要編碼的字元數。
- bytes
- Byte[]
這個位元組陣列用來包含產生的位元組序列。
- byteIndex
- Int32
索引開始寫入產生的位元組序列。
傳回
寫入 bytes的實際位元組數。
- 屬性
例外狀況
charIndex 或 charCount 或 byteIndex 小於零。
-或-
charIndex 且 charCount 不表示在 中的 s有效範圍。
-或-
byteIndex 在 中 不是有效的指標 bytes。
bytes 從陣列到末端的容量 byteIndex 不足以容納產生的位元組。
隨後出現了備用機制(詳見字元編碼.NET的詳細說明)。
-及-
範例
以下程式碼範例示範如何使用此 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方法或GetEncoder方法所提供的GetDecoder。
Note
UTF7Encoding 無法提供錯誤偵測。 無效字元則以修改過的 64 進位字元編碼。 出於安全考量,建議您的應用程式使用 UTF8Encoding、 , UnicodeEncoding或 UTF32Encoding 啟用錯誤偵測功能。