UnicodeEncoding.GetBytes 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將一組字元編碼成位元組序列。
多載
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 中的字元編碼) -和- EncoderFallback 設定為 EncoderExceptionFallback。
範例
下列範例示範如何使用 GetBytes 方法,從 String 編碼字元範圍,並將編碼的位元組儲存在位元組陣列中的元素範圍內。
using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
array<Byte>^bytes;
String^ chars = "Unicode Encoding Example";
UnicodeEncoding^ Unicode = gcnew UnicodeEncoding;
int byteCount = Unicode->GetByteCount( chars->ToCharArray(), 8, 8 );
bytes = gcnew array<Byte>(byteCount);
int bytesEncodedCount = Unicode->GetBytes( chars, 8, 8, bytes, 0 );
Console::WriteLine( "{0} bytes used to encode string.", 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 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 。 如果沒有錯誤偵測,則會忽略不正確序列,而且不會擲回任何例外狀況。
要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分成較小的區塊,應用程式應該分別使用 Decoder 方法 Encoder 或 方法所提供的 GetDecoder 。 GetEncoder
重要
若要確保編碼的位元組在儲存為檔案或資料流程時正確解碼,您可以將編碼位元組的資料流程加上前置詞。 在位元組資料流程開頭插入前置詞 (,例如要寫入檔案的一系列位元組開頭,) 是開發人員的責任。 方法 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
不代表 chars
中有效的範圍。
-或-
byteIndex
在 bytes
中不是有效的索引。
發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- EncoderFallback 設定為 EncoderExceptionFallback。
範例
下列範例示範如何從 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'};
UnicodeEncoding^ Unicode = gcnew UnicodeEncoding;
int byteCount = Unicode->GetByteCount( chars, 1, 2 );
bytes = gcnew array<Byte>(byteCount);
int bytesEncodedCount = Unicode->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 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 。 如果沒有錯誤偵測,則會忽略不正確序列,而且不會擲回任何例外狀況。
要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分成較小的區塊,應用程式應該分別使用 Decoder 方法 Encoder 或 方法所提供的 GetDecoder 。 GetEncoder
重要
若要確保編碼的位元組在儲存為檔案或資料流程時正確解碼,您可以將編碼位元組的資料流程加上前置詞。 在位元組資料流程開頭插入前置詞 (,例如要寫入檔案的一系列位元組開頭,) 是開發人員的責任。 方法 GetBytes 不會在前置詞前面加上編碼位元組序列的開頭。
另請參閱
適用於
GetBytes(String)
GetBytes(Char*, Int32, Byte*, Int32)
重要
此 API 不符合 CLS 規範。
將起始於指定字元指標的一組字元編碼成位元組序列;儲存該位元組序列時,係以指定的位元組指標為起始點。
public:
override int GetBytes(char* chars, int charCount, System::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]
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)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<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
參數
- chars
- Char*
要編碼的第一個字元指標。
- charCount
- Int32
要編碼的字元數。
- bytes
- Byte*
開始寫入結果位元組序列的位置指標。
- byteCount
- Int32
寫入的最大位元組數。
傳回
bytes
參數所指示位置上寫入的實際位元組數目。
- 屬性
例外狀況
charCount
或 byteCount
小於零。
發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- EncoderFallback 設定為 EncoderExceptionFallback。
備註
若要計算儲存所產生位元組所需的確切陣列大小 GetBytes ,請呼叫 GetByteCount 方法。 若要計算陣列大小上限,請呼叫 GetMaxByteCount 方法。 方法 GetByteCount 通常會配置較少的記憶體,而 GetMaxByteCount 方法通常執行速度較快。
使用錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 。 如果沒有錯誤偵測,則會忽略不正確序列,而且不會擲回任何例外狀況。
要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分成較小的區塊,應用程式應該分別使用 Decoder 或 方法所提供的 GetDecoder 或 Encoder GetEncoder 物件。
重要
若要確保編碼的位元組在儲存為檔案或資料流程時正確解碼,您可以將編碼位元組的資料流程加上前置詞。 在位元組資料流程開頭插入前置詞 (,例如要寫入檔案的一系列位元組開頭,) 是開發人員的責任。 方法 GetBytes 不會在前置詞前面加上編碼位元組序列的開頭。