UTF8Encoding.GetBytes 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將一組字元編碼成一串位元組。
多載
| 名稱 | Description |
|---|---|
| GetBytes(Char[], Int32, Int32, Byte[], Int32) |
將指定字元陣列中的一組字元編碼到指定的位元組陣列中。 |
| GetBytes(String, Int32, Int32, Byte[], Int32) |
將指定的 String 一組字元編碼到指定的位元組陣列中。 |
| GetBytes(String) |
將指定 String 物件中的字元編碼成一串位元組。 |
| GetBytes(Char*, Int32, Byte*, Int32) |
將一組從指定字元指標開始的字元編碼成一串位元組,並從指定位元組指標開始儲存。 |
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。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例使用此 GetBytes 方法從字串編碼一組字元,並將編碼的位元組儲存在位元組陣列中的元素範圍內。
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 異常。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量過大需要分割成較小區塊,則分別使用DecoderEncoder該方法或GetEncoder該方法所提供的GetDecoder。
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流的開頭插入前導詞(例如在一連串要寫入檔案的位元組開頭)是開發者的責任。 此 GetBytes 方法不會在編碼位元組序列的開頭前加上前言。
另請參閱
適用於
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);
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。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例使用此 GetBytes 方法,從 Unicode 字元陣列編碼元素範圍,並將編碼的位元組儲存在位元組陣列中的元素範圍內。
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 異常。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量過大需要分割成較小區塊,則分別使用DecoderEncoder該方法或GetEncoder該方法所提供的GetDecoder。
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流的開頭插入前導詞(例如在一連串要寫入檔案的位元組開頭)是開發者的責任。 此 GetBytes 方法不會在編碼位元組序列的開頭前加上前言。
另請參閱
適用於
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
要編碼的字串。
傳回
一個位元組陣列,包含由 s 參數指定的字串中編碼的字元。
適用於
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 小於零。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
備註
要計算儲存所得位元組所需的 GetBytes 精確陣列大小,你就呼叫了 方法 GetByteCount 。 要計算最大陣列大小,你呼叫 方法 GetMaxByteCount 。 此 GetByteCount 方法通常分配較少記憶體,而 GetMaxByteCount 執行速度較快。
在錯誤偵測中,若序列無效,該方法會拋出 ArgumentException 異常。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量過大需要分割成較小區塊,則分別使用DecoderEncoder方法GetEncoder或方法回傳GetDecoder的 。
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流的開頭插入前導詞(例如在一連串要寫入檔案的位元組開頭)是開發者的責任。 此 GetBytes 方法不會在編碼位元組序列的開頭前加上前言。