UnicodeEncoding.GetBytes 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將一組字元編碼成一串位元組。
多載
| 名稱 | Description |
|---|---|
| GetBytes(Char[], Int32, Int32, Byte[], Int32) |
將指定字元陣列中的一組字元編碼到指定的位元組陣列中。 |
| GetBytes(String, Int32, Int32, Byte[], Int32) |
將指定的 String 一組字元編碼到指定的位元組陣列中。 |
| GetBytes(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 方法編碼從 中的 String 一組字元,並將編碼的位元組儲存在位元組陣列中的元素範圍內。
using System;
using System.Text;
class UnicodeEncodingExample {
public static void Main() {
Byte[] bytes;
String chars = "Unicode Encoding Example";
UnicodeEncoding Unicode = new UnicodeEncoding();
int byteCount = Unicode.GetByteCount(chars.ToCharArray(), 8, 8);
bytes = new Byte[byteCount];
int bytesEncodedCount = Unicode.GetBytes(chars, 8, 8, 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 UnicodeEncodingExample
Public Shared Sub Main()
Dim bytes() As Byte
Dim chars As String = "Unicode Encoding Example"
Dim uni As New UnicodeEncoding()
Dim byteCount As Integer = uni.GetByteCount(chars.ToCharArray(), 8, 8)
bytes = New Byte(byteCount - 1) {}
Dim bytesEncodedCount As Integer = uni.GetBytes(chars, 8, 8, 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。
Important
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流的開頭插入前導詞(例如在一連串要寫入檔案的位元組開頭)是開發者的責任。 此 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
參數
- s
- String
包含要編碼字元集合的字串。
- charIndex
- Int32
第一個要編碼字元的索引。
- charCount
- Int32
需要編碼的字元數。
- bytes
- Byte[]
這個位元組陣列用來包含產生的位元組序列。
- byteIndex
- Int32
索引開始寫入產生的位元組序列。
傳回
寫入 bytes的實際位元組數。
例外狀況
charIndex 或 charCount 或 byteIndex 小於零。
-或-
charIndex 且 charCount 不表示在 中的 s有效範圍。
-或-
byteIndex 在 中 不是有效的指標 bytes。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例示範如何從 Unicode 字元陣列編碼元素範圍,並將編碼的位元組儲存在位元組陣列中的元素範圍內。
using System;
using System.Text;
class UnicodeEncodingExample {
public static void Main() {
Byte[] bytes;
// Unicode characters.
Char[] chars = new Char[] {
'\u0023', // #
'\u0025', // %
'\u03a0', // Pi
'\u03a3' // Sigma
};
UnicodeEncoding Unicode = new UnicodeEncoding();
int byteCount = Unicode.GetByteCount(chars, 1, 2);
bytes = new Byte[byteCount];
int bytesEncodedCount = Unicode.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 UnicodeEncodingExample
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 uni As New UnicodeEncoding()
Dim byteCount As Integer = uni.GetByteCount(chars, 1, 2)
bytes = New Byte(byteCount - 1) {}
Dim bytesEncodedCount As Integer = uni.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。
Important
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流的開頭插入前導詞(例如在一連串要寫入檔案的位元組開頭)是開發者的責任。 此 GetBytes 方法不會在編碼位元組序列的開頭前加上前言。
另請參閱
適用於
GetBytes(Char*, Int32, Byte*, Int32)
重要
此 API 不符合 CLS 規範。
將一組從指定字元指標開始的字元編碼成一串位元組,並從指定位元組指標開始儲存。
public:
override int GetBytes(char* chars, int charCount, System::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.Runtime.InteropServices.ComVisible(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)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(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
參數
- chars
- Char*
一個指向第一個要編碼字元的指標。
- charCount
- Int32
需要編碼的字元數。
- bytes
- Byte*
一個指向開始寫入產生的位元組序列位置的指標。
- byteCount
- Int32
最大可寫入位元組數。
傳回
參數所示 bytes 位置實際寫入的位元組數。
- 屬性
例外狀況
charCount 或 byteCount 小於零。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
備註
要計算儲存產生的位元組所需的精確陣列大小 GetBytes ,你就呼叫這個 GetByteCount 方法。 要計算最大陣列大小,你呼叫 方法 GetMaxByteCount 。 此 GetByteCount 方法通常分配較少記憶體,而 GetMaxByteCount 執行速度較快。
在錯誤檢測中,若序列無效,該方法會拋出 ArgumentException。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。
待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量過大需要分割成較小區塊時,應用程式應分別使用 Decoder 或 Encoder 或 方法所提供的GetDecoderGetEncoder物件。
Important
為了確保編碼的位元組在儲存為檔案或串流時能正確解碼,你可以在編碼位元組串流前加上前言。 在位元組串流的開頭插入前導詞(例如在一連串要寫入檔案的位元組開頭)是開發者的責任。 此 GetBytes 方法不會在編碼位元組序列的開頭前加上前言。