Encoding.GetBytes 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當在衍生類別中被覆寫時,會將一組字元編碼成一串位元組。
多載
| 名稱 | Description |
|---|---|
| GetBytes(Char[]) |
當在派生類別中覆寫時,會將指定字元陣列中的所有字元編碼成一串位元組。 |
| GetBytes(String) |
當在派生類別中覆寫時,會將指定字串中的所有字元編碼成一串位元組。 |
| GetBytes(ReadOnlySpan<Char>, Span<Byte>) |
當在派生類別中覆寫時,會將指定唯讀區間的一組字元編碼到位元組區間。 |
| GetBytes(Char[], Int32, Int32) |
當在派生類別中覆寫時,會將指定字元陣列中的一組字元編碼成一串位元組。 |
| GetBytes(String, Int32, Int32) |
當在派生類別中覆寫時,會將指定字串中指定的 |
| GetBytes(Char*, Int32, Byte*, Int32) |
當在派生類別中覆寫時,會將一組從指定字元指標開始的字元編碼成一列位元組,並從指定位元組指標開始儲存。 |
| GetBytes(Char[], Int32, Int32, Byte[], Int32) |
當在派生類別中覆寫時,會將指定字元陣列中的一組字元編碼到指定的位元組陣列中。 |
| GetBytes(String, Int32, Int32, Byte[], Int32) |
當在派生類別中覆寫時,會將指定字串中的一組字元編碼到指定的位元組陣列中。 |
GetBytes(Char[])
當在派生類別中覆寫時,會將指定字元陣列中的所有字元編碼成一串位元組。
public:
virtual cli::array <System::Byte> ^ GetBytes(cli::array <char> ^ chars);
public virtual byte[] GetBytes(char[] chars);
abstract member GetBytes : char[] -> byte[]
override this.GetBytes : char[] -> byte[]
Public Overridable Function GetBytes (chars As Char()) As Byte()
參數
- chars
- Char[]
包含要編碼字元的字元陣列。
傳回
一個位元組陣列,包含編碼指定字元集合的結果。
例外狀況
chars 是 null。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例決定編碼字元陣列所需的位元組數,編碼字元,並顯示產生的位元組。
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// The characters to encode:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
// a high-surrogate value (U+D8FF)
// a low-surrogate value (U+DCFF)
char[] myChars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
// Get different encodings.
Encoding u7 = Encoding.UTF7;
Encoding u8 = Encoding.UTF8;
Encoding u16LE = Encoding.Unicode;
Encoding u16BE = Encoding.BigEndianUnicode;
Encoding u32 = Encoding.UTF32;
// Encode the entire array, and print out the counts and the resulting bytes.
PrintCountsAndBytes( myChars, u7 );
PrintCountsAndBytes( myChars, u8 );
PrintCountsAndBytes( myChars, u16LE );
PrintCountsAndBytes( myChars, u16BE );
PrintCountsAndBytes( myChars, u32 );
}
public static void PrintCountsAndBytes( char[] chars, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( chars );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( chars.Length );
Console.Write( " {0,-3} :", iMBC );
// Encode the array of chars.
byte[] bytes = enc.GetBytes( chars );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintHexBytes( byte[] bytes ) {
if (( bytes == null ) || ( bytes.Length == 0 ))
{
Console.WriteLine( "<none>" );
}
else {
for ( int i = 0; i < bytes.Length; i++ )
Console.Write( "{0:X2} ", bytes[i] );
Console.WriteLine();
}
}
}
/*
This code produces the following output.
System.Text.UTF7Encoding : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
System.Text.UTF8Encoding : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' The characters to encode:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
' a high-surrogate value (U+D8FF)
' a low-surrogate value (U+DCFF)
Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2), ChrW(&HD8FF), ChrW(&HDCFF)}
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode the entire array, and print out the counts and the resulting bytes.
PrintCountsAndBytes(myChars, u7)
PrintCountsAndBytes(myChars, u8)
PrintCountsAndBytes(myChars, u16LE)
PrintCountsAndBytes(myChars, u16BE)
PrintCountsAndBytes(myChars, u32)
End Sub
Public Shared Sub PrintCountsAndBytes(chars() As Char, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(chars)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(chars.Length)
Console.Write(" {0,-3} :", iMBC)
' Encode the array of chars.
Dim bytes As Byte() = enc.GetBytes(chars)
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'System.Text.UTF7Encoding : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
'System.Text.UTF8Encoding : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00
備註
如果要轉換的資料僅以循序區塊(例如從資料流讀取的數據)形式提供,或資料量太大而需要分割成較小的區塊,則您應該使用由衍生類別的方法 Decoder 提供的 Encoder 或方法 GetDecoder 提供的 GetEncoder。
該 GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。 此 GetBytes 方法預期離散轉換,與 Encoder.GetBytes 處理單一輸入串流多重轉換的方法不同。
支援多個版本 GetByteCount 的 and GetBytes 。 以下是使用這些方法時的一些程式設計考量:
你的應用程式可能需要將多個輸入字元編碼到一個代碼頁,並透過多次呼叫來處理這些字元。 在這種情況下,你可能需要在呼叫間維持狀態,並考慮所使用物件所持久 Encoder 化的狀態。 (例如,包含替代對的字元序列可能以高代理結尾。他們 Encoder 會記住那個高代理,這樣在下一通電話開始時就能和低代身合併。 Encoding 無法維持該狀態,因此該角色會被送往 EncoderFallback。)
如果你的應用程式處理字串輸入,你應該呼叫該方法的 GetBytes 字串版本。
Unicode 字元緩衝區 GetBytes(Char*, Int32, Byte*, Int32) 版本允許一些快速技術,特別是在多次呼叫物件 Encoder 或插入現有緩衝區時。 不過請注意,此方法版本有時不安全,因為需要指標。
如果你的應用程式需要轉換大量資料,它應該會重複使用輸出緩衝區。 在這種情況下, GetBytes 支援位元組陣列的版本是最佳選擇。
考慮使用該 Encoder.Convert 方法而非 GetByteCount。 轉換方法會盡可能轉換大量資料,若輸出緩衝區過小則會拋出例外。 對於串流的連續編碼,這種方法通常是最佳選擇。
另請參閱
適用於
GetBytes(String)
當在派生類別中覆寫時,會將指定字串中的所有字元編碼成一串位元組。
public:
virtual cli::array <System::Byte> ^ GetBytes(System::String ^ s);
public virtual byte[] GetBytes(string s);
abstract member GetBytes : string -> byte[]
override this.GetBytes : string -> byte[]
Public Overridable Function GetBytes (s As String) As Byte()
參數
- s
- String
包含要編碼字元的字串。
傳回
一個位元組陣列,包含編碼指定字元集合的結果。
例外狀況
s 是 null。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例決定編碼字串或字串範圍所需的位元組數,編碼字元,並顯示產生的位元組。
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// The characters to encode:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
// a high-surrogate value (U+D8FF)
// a low-surrogate value (U+DCFF)
String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
// Get different encodings.
Encoding u7 = Encoding.UTF7;
Encoding u8 = Encoding.UTF8;
Encoding u16LE = Encoding.Unicode;
Encoding u16BE = Encoding.BigEndianUnicode;
Encoding u32 = Encoding.UTF32;
// Encode the entire string, and print out the counts and the resulting bytes.
Console.WriteLine( "Encoding the entire string:" );
PrintCountsAndBytes( myStr, u7 );
PrintCountsAndBytes( myStr, u8 );
PrintCountsAndBytes( myStr, u16LE );
PrintCountsAndBytes( myStr, u16BE );
PrintCountsAndBytes( myStr, u32 );
Console.WriteLine();
// Encode three characters starting at index 4, and print out the counts and the resulting bytes.
Console.WriteLine( "Encoding the characters from index 4 through 6:" );
PrintCountsAndBytes( myStr, 4, 3, u7 );
PrintCountsAndBytes( myStr, 4, 3, u8 );
PrintCountsAndBytes( myStr, 4, 3, u16LE );
PrintCountsAndBytes( myStr, 4, 3, u16BE );
PrintCountsAndBytes( myStr, 4, 3, u32 );
}
public static void PrintCountsAndBytes( String s, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( s );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( s.Length );
Console.Write( " {0,-3} :", iMBC );
// Encode the entire string.
byte[] bytes = enc.GetBytes( s );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintCountsAndBytes( String s, int index, int count, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( s.ToCharArray(), index, count );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( count );
Console.Write( " {0,-3} :", iMBC );
// Encode a range of characters in the string.
byte[] bytes = new byte[iBC];
enc.GetBytes( s, index, count, bytes, bytes.GetLowerBound(0) );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintHexBytes( byte[] bytes ) {
if (( bytes == null ) || ( bytes.Length == 0 ))
{
Console.WriteLine( "<none>" );
}
else {
for ( int i = 0; i < bytes.Length; i++ )
Console.Write( "{0:X2} ", bytes[i] );
Console.WriteLine();
}
}
}
/*
This code produces the following output.
Encoding the entire string:
System.Text.UTF7Encoding : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
System.Text.UTF8Encoding : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00
Encoding the characters from index 4 through 6:
System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' The characters to encode:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
' a high-surrogate value (U+D8FF)
' a low-surrogate value (U+DCFF)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode the entire string, and print out the counts and the resulting bytes.
Console.WriteLine("Encoding the entire string:")
PrintCountsAndBytes(myStr, u7)
PrintCountsAndBytes(myStr, u8)
PrintCountsAndBytes(myStr, u16LE)
PrintCountsAndBytes(myStr, u16BE)
PrintCountsAndBytes(myStr, u32)
Console.WriteLine()
' Encode three characters starting at index 4, and print out the counts and the resulting bytes.
Console.WriteLine("Encoding the characters from index 4 through 6:")
PrintCountsAndBytes(myStr, 4, 3, u7)
PrintCountsAndBytes(myStr, 4, 3, u8)
PrintCountsAndBytes(myStr, 4, 3, u16LE)
PrintCountsAndBytes(myStr, 4, 3, u16BE)
PrintCountsAndBytes(myStr, 4, 3, u32)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(s.Length)
Console.Write(" {0,-3} :", iMBC)
' Encode the entire string.
Dim bytes As Byte() = enc.GetBytes(s)
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s.ToCharArray(), index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode a range of characters in the string.
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
Dim bytes(iBC - 1) As Byte
enc.GetBytes(s, index, count, bytes, bytes.GetLowerBound(0))
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'Encoding the entire string:
'System.Text.UTF7Encoding : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
'System.Text.UTF8Encoding : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00
'
'Encoding the characters from index 4 through 6:
'System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
'System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
備註
如果要轉換的資料僅以循序區塊(例如從資料流讀取的數據)形式提供,或資料量太大而需要分割成較小的區塊,則您應該使用由衍生類別的方法 Decoder 提供的 Encoder 或方法 GetDecoder 提供的 GetEncoder。
該 GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。 此 Encoding.GetBytes 方法預期離散轉換,與 Encoder.GetBytes 處理單一輸入串流多重轉換的方法不同。
支援多個版本 GetByteCount 的 and GetBytes 。 以下是使用這些方法時的一些程式設計考量:
你的應用程式可能需要將多個輸入字元編碼到一個代碼頁,並透過多次呼叫來處理這些字元。 在這種情況下,你可能需要在呼叫間維持狀態,並考慮所使用物件所持久 Encoder 化的狀態。 (例如,包含替代對的字元序列可能以高代理結尾。他們 Encoder 會記住那個高代理,這樣在下一通電話開始時就能和低代身合併。 Encoding 無法維持該狀態,因此該角色會被送往 EncoderFallback。)
如果你的應用程式處理字串輸入,你應該使用字串版本 GetBytes。
Unicode 字元緩衝區 GetBytes(Char*, Int32, Byte*, Int32) 版本允許一些快速技術,特別是在多次呼叫物件 Encoder 或插入現有緩衝區時。 不過請注意,此方法版本有時不安全,因為需要指標。
如果你的應用程式需要轉換大量資料,它應該會重複使用輸出緩衝區。 在這種情況下, GetBytes 支援位元組陣列的版本是最佳選擇。
考慮使用該 Encoder.Convert 方法而非 GetByteCount。 轉換方法會盡可能轉換大量資料,若輸出緩衝區過小則會拋出例外。 對於串流的連續編碼,這種方法通常是最佳選擇。
另請參閱
適用於
GetBytes(ReadOnlySpan<Char>, Span<Byte>)
當在派生類別中覆寫時,會將指定唯讀區間的一組字元編碼到位元組區間。
public:
virtual int GetBytes(ReadOnlySpan<char> chars, Span<System::Byte> bytes);
public virtual int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes);
abstract member GetBytes : ReadOnlySpan<char> * Span<byte> -> int
override this.GetBytes : ReadOnlySpan<char> * Span<byte> -> int
Public Overridable Function GetBytes (chars As ReadOnlySpan(Of Char), bytes As Span(Of Byte)) As Integer
參數
- chars
- ReadOnlySpan<Char>
包含要編碼字元集合的範圍。
傳回
編碼的位元組數。
備註
如果要轉換的資料僅以循序區塊(例如從資料流讀取的數據)形式提供,或資料量太大而需要分割成較小的區塊,則您應該使用由衍生類別的方法 Decoder 提供的 Encoder 或方法 GetDecoder 提供的 GetEncoder。
該 GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。 此 Encoding.GetBytes 方法預期離散轉換,與 Encoder.GetBytes 處理單一輸入串流多重轉換的方法不同。
支援多個版本 GetByteCount 的 and GetBytes 。 以下是使用這些方法時的一些程式設計考量:
你的應用程式可能需要將多個輸入字元編碼到一個代碼頁,並透過多次呼叫來處理這些字元。 在這種情況下,你可能需要在呼叫間維持狀態,並考慮所使用物件所持久 Encoder 化的狀態。 (例如,包含替代對的字元序列可能以高代理結尾。他們 Encoder 會記住那個高代理,這樣在下一通電話開始時就能和低代身合併。 Encoding 無法維持該狀態,因此該角色會被送往 EncoderFallback。)
如果你的應用程式處理字串輸入,你應該使用字串版本 GetBytes。
如果你的應用程式需要轉換大量資料,它應該會重複使用輸出緩衝區。 在這種情況下, GetBytes 支援位元組陣列的版本是最佳選擇。
考慮使用該 Encoder.Convert 方法而非 GetByteCount。 轉換方法會盡可能轉換大量資料,若輸出緩衝區過小則會拋出例外。 對於串流的連續編碼,這種方法通常是最佳選擇。
適用於
GetBytes(Char[], Int32, Int32)
當在派生類別中覆寫時,會將指定字元陣列中的一組字元編碼成一串位元組。
public:
virtual cli::array <System::Byte> ^ GetBytes(cli::array <char> ^ chars, int index, int count);
public virtual byte[] GetBytes(char[] chars, int index, int count);
abstract member GetBytes : char[] * int * int -> byte[]
override this.GetBytes : char[] * int * int -> byte[]
Public Overridable Function GetBytes (chars As Char(), index As Integer, count As Integer) As Byte()
參數
- chars
- Char[]
包含要編碼字元集合的字元陣列。
- index
- Int32
第一個要編碼字元的索引。
- count
- Int32
需要編碼的字元數。
傳回
一個位元組陣列,包含編碼指定字元集合的結果。
例外狀況
chars 是 null。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例決定從字元陣列編碼三個字元所需的位元組數,編碼字元,並顯示所得位元組。
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// The characters to encode:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
// a high-surrogate value (U+D8FF)
// a low-surrogate value (U+DCFF)
char[] myChars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
// Get different encodings.
Encoding u7 = Encoding.UTF7;
Encoding u8 = Encoding.UTF8;
Encoding u16LE = Encoding.Unicode;
Encoding u16BE = Encoding.BigEndianUnicode;
Encoding u32 = Encoding.UTF32;
// Encode three characters starting at index 4, and print out the counts and the resulting bytes.
PrintCountsAndBytes( myChars, 4, 3, u7 );
PrintCountsAndBytes( myChars, 4, 3, u8 );
PrintCountsAndBytes( myChars, 4, 3, u16LE );
PrintCountsAndBytes( myChars, 4, 3, u16BE );
PrintCountsAndBytes( myChars, 4, 3, u32 );
}
public static void PrintCountsAndBytes( char[] chars, int index, int count, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( chars, index, count );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( count );
Console.Write( " {0,-3} :", iMBC );
// Encode the array of chars.
byte[] bytes = enc.GetBytes( chars, index, count );
// The following is an alternative way to encode the array of chars:
// byte[] bytes = new byte[iBC];
// enc.GetBytes( chars, index, count, bytes, bytes.GetLowerBound(0) );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintHexBytes( byte[] bytes ) {
if (( bytes == null ) || ( bytes.Length == 0 ))
{
Console.WriteLine( "<none>" );
}
else {
for ( int i = 0; i < bytes.Length; i++ )
Console.Write( "{0:X2} ", bytes[i] );
Console.WriteLine();
}
}
}
/*
This code produces the following output.
System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' The characters to encode:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
' a high-surrogate value (U+D8FF)
' a low-surrogate value (U+DCFF)
Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2), ChrW(&HD8FF), ChrW(&HDCFF) }
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode three characters starting at index 4, and print out the counts and the resulting bytes.
PrintCountsAndBytes(myChars, 4, 3, u7)
PrintCountsAndBytes(myChars, 4, 3, u8)
PrintCountsAndBytes(myChars, 4, 3, u16LE)
PrintCountsAndBytes(myChars, 4, 3, u16BE)
PrintCountsAndBytes(myChars, 4, 3, u32)
End Sub
Public Shared Sub PrintCountsAndBytes(chars() As Char, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(chars, index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode the array of chars.
Dim bytes As Byte() = enc.GetBytes(chars, index, count)
' The following is an alternative way to encode the array of chars:
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
' Dim bytes(iBC - 1) As Byte
' enc.GetBytes( chars, index, count, bytes, bytes.GetLowerBound(0) )
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
'System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
備註
如果要轉換的資料僅以循序區塊(例如從資料流讀取的數據)形式提供,或資料量太大而需要分割成較小的區塊,則您應該使用由衍生類別的方法 Decoder 提供的 Encoder 或方法 GetDecoder 提供的 GetEncoder。
該 GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。 此 Encoding.GetBytes 方法預期離散轉換,與 Encoder.GetBytes 處理單一輸入串流多重轉換的方法不同。
支援多個版本 GetByteCount 的 and GetBytes 。 以下是使用這些方法時的一些程式設計考量:
你的應用程式可能需要將多個輸入字元編碼到一個代碼頁,並透過多次呼叫來處理這些字元。 在這種情況下,你可能需要在呼叫間維持狀態,並考慮所使用物件所持久 Encoder 化的狀態。 (例如,包含替代對的字元序列可能以高代理結尾。他們 Encoder 會記住那個高代理,這樣在下一通電話開始時就能和低代身合併。 Encoding 無法維持該狀態,因此該角色會被送往 EncoderFallback。)
如果你的應用程式處理字串輸入,你應該使用字串版本 GetBytes。
Unicode 字元緩衝區 GetBytes(Char*, Int32, Byte*, Int32) 版本允許一些快速技術,特別是在多次呼叫物件 Encoder 或插入現有緩衝區時。 不過請注意,此方法版本有時不安全,因為需要指標。
如果你的應用程式需要轉換大量資料,它應該會重複使用輸出緩衝區。 在這種情況下, GetBytes 支援位元組陣列的版本是最佳選擇。
考慮使用該 Encoder.Convert 方法而非 GetByteCount。 轉換方法會盡可能轉換大量資料,若輸出緩衝區過小則會拋出例外。 對於串流的連續編碼,這種方法通常是最佳選擇。
另請參閱
適用於
GetBytes(String, Int32, Int32)
當在派生類別中覆寫時,會將指定字串中指定的 count 字元數編碼到一個位元組陣列中,從指定的 index字串開始。
public:
cli::array <System::Byte> ^ GetBytes(System::String ^ s, int index, int count);
public byte[] GetBytes(string s, int index, int count);
member this.GetBytes : string * int * int -> byte[]
Public Function GetBytes (s As String, index As Integer, count As Integer) As Byte()
參數
- s
- String
包含要編碼字元的字串。
- index
- Int32
字串內部的索引,用來開始編碼。
- count
- Int32
需要編碼的字元數。
傳回
一個位元組陣列,包含編碼指定字元集合的結果。
範例
以下範例決定編碼字串或字串範圍所需的位元組數,編碼字元,並顯示產生的位元組。
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// The characters to encode:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
// a high-surrogate value (U+D8FF)
// a low-surrogate value (U+DCFF)
String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
// Get different encodings.
Encoding u7 = Encoding.UTF7;
Encoding u8 = Encoding.UTF8;
Encoding u16LE = Encoding.Unicode;
Encoding u16BE = Encoding.BigEndianUnicode;
Encoding u32 = Encoding.UTF32;
// Encode the entire string, and print out the counts and the resulting bytes.
Console.WriteLine( "Encoding the entire string:" );
PrintCountsAndBytes( myStr, u7 );
PrintCountsAndBytes( myStr, u8 );
PrintCountsAndBytes( myStr, u16LE );
PrintCountsAndBytes( myStr, u16BE );
PrintCountsAndBytes( myStr, u32 );
Console.WriteLine();
// Encode three characters starting at index 4, and print out the counts and the resulting bytes.
Console.WriteLine( "Encoding the characters from index 4 through 6:" );
PrintCountsAndBytes( myStr, 4, 3, u7 );
PrintCountsAndBytes( myStr, 4, 3, u8 );
PrintCountsAndBytes( myStr, 4, 3, u16LE );
PrintCountsAndBytes( myStr, 4, 3, u16BE );
PrintCountsAndBytes( myStr, 4, 3, u32 );
}
public static void PrintCountsAndBytes( String s, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( s );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( s.Length );
Console.Write( " {0,-3} :", iMBC );
// Encode the entire string.
byte[] bytes = enc.GetBytes( s );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintCountsAndBytes( String s, int index, int count, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( s.ToCharArray(), index, count );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( count );
Console.Write( " {0,-3} :", iMBC );
// Encode a range of characters in the string.
byte[] bytes = new byte[iBC];
enc.GetBytes( s, index, count, bytes, bytes.GetLowerBound(0) );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintHexBytes( byte[] bytes ) {
if (( bytes == null ) || ( bytes.Length == 0 ))
{
Console.WriteLine( "<none>" );
}
else {
for ( int i = 0; i < bytes.Length; i++ )
Console.Write( "{0:X2} ", bytes[i] );
Console.WriteLine();
}
}
}
/*
This code produces the following output.
Encoding the entire string:
System.Text.UTF7Encoding : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
System.Text.UTF8Encoding : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00
Encoding the characters from index 4 through 6:
System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' The characters to encode:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
' a high-surrogate value (U+D8FF)
' a low-surrogate value (U+DCFF)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode the entire string, and print out the counts and the resulting bytes.
Console.WriteLine("Encoding the entire string:")
PrintCountsAndBytes(myStr, u7)
PrintCountsAndBytes(myStr, u8)
PrintCountsAndBytes(myStr, u16LE)
PrintCountsAndBytes(myStr, u16BE)
PrintCountsAndBytes(myStr, u32)
Console.WriteLine()
' Encode three characters starting at index 4, and print out the counts and the resulting bytes.
Console.WriteLine("Encoding the characters from index 4 through 6:")
PrintCountsAndBytes(myStr, 4, 3, u7)
PrintCountsAndBytes(myStr, 4, 3, u8)
PrintCountsAndBytes(myStr, 4, 3, u16LE)
PrintCountsAndBytes(myStr, 4, 3, u16BE)
PrintCountsAndBytes(myStr, 4, 3, u32)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(s.Length)
Console.Write(" {0,-3} :", iMBC)
' Encode the entire string.
Dim bytes As Byte() = enc.GetBytes(s)
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s.ToCharArray(), index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode a range of characters in the string.
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
Dim bytes(iBC - 1) As Byte
enc.GetBytes(s, index, count, bytes, bytes.GetLowerBound(0))
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'Encoding the entire string:
'System.Text.UTF7Encoding : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
'System.Text.UTF8Encoding : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00
'
'Encoding the characters from index 4 through 6:
'System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
'System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
備註
如果要轉換的資料僅以循序區塊(例如從資料流讀取的數據)形式提供,或資料量太大而需要分割成較小的區塊,則您應該使用由衍生類別的方法 Decoder 提供的 Encoder 或方法 GetDecoder 提供的 GetEncoder。
該 GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。 此 Encoding.GetBytes 方法預期離散轉換,與 Encoder.GetBytes 處理單一輸入串流多重轉換的方法不同。
支援多個版本 GetByteCount 的 and GetBytes 。 以下是使用這些方法時的一些程式設計考量:
你的應用程式可能需要將多個輸入字元編碼到一個代碼頁,並透過多次呼叫來處理這些字元。 在這種情況下,你可能需要在呼叫間維持狀態,並考慮所使用物件所持久 Encoder 化的狀態。 (例如,包含替代對的字元序列可能以高代理結尾。他們 Encoder 會記住那個高代理,這樣在下一通電話開始時就能和低代身合併。 Encoding 無法維持該狀態,因此該角色會被送往 EncoderFallback。)
如果你的應用程式處理字串輸入,你應該使用字串版本 GetBytes。
Unicode 字元緩衝區 GetBytes(Char*, Int32, Byte*, Int32) 版本允許一些快速技術,特別是在多次呼叫物件 Encoder 或插入現有緩衝區時。 不過請注意,此方法版本有時不安全,因為需要指標。
如果你的應用程式需要轉換大量資料,它應該會重複使用輸出緩衝區。 在這種情況下, GetBytes 支援位元組陣列的版本是最佳選擇。
考慮使用該 Encoder.Convert 方法而非 GetByteCount。 轉換方法會盡可能轉換大量資料,若輸出緩衝區過小則會拋出例外。 對於串流的連續編碼,這種方法通常是最佳選擇。
適用於
GetBytes(Char*, Int32, Byte*, Int32)
重要
此 API 不符合 CLS 規範。
當在派生類別中覆寫時,會將一組從指定字元指標開始的字元編碼成一列位元組,並從指定位元組指標開始儲存。
public:
virtual int GetBytes(char* chars, int charCount, System::Byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
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衍生類別中 或 的方法所提供的GetDecoderGetEncoder物件。
該 GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。 此 GetBytes 方法預期離散轉換,與 Encoder.GetBytes 處理單一輸入串流多重轉換的方法不同。
支援多個版本 GetByteCount 的 and GetBytes 。 以下是使用這些方法時的一些程式設計考量:
你的應用程式可能需要將多個輸入字元編碼到一個代碼頁,並透過多次呼叫來處理這些字元。 在這種情況下,你可能需要在呼叫間維持狀態,並考慮所使用物件所持久 Encoder 化的狀態。 (例如,包含替代對的字元序列可能以高代理結尾。他們 Encoder 會記住那個高代理,這樣在下一通電話開始時就能和低代身合併。 Encoding 無法維持該狀態,因此該角色會被送往 EncoderFallback。)
如果你的應用程式處理字串輸入,你應該使用字串版本 GetBytes。
Unicode 字元緩衝區 GetBytes(Char*, Int32, Byte*, Int32) 版本允許一些快速技術,特別是在多次呼叫物件 Encoder 或插入現有緩衝區時。 不過請注意,此方法版本有時不安全,因為需要指標。
如果你的應用程式需要轉換大量資料,它應該會重複使用輸出緩衝區。 在這種情況下, GetBytes 支援位元組陣列的版本是最佳選擇。
考慮使用該 Encoder.Convert 方法而非 GetByteCount。 轉換方法會盡可能轉換大量資料,若輸出緩衝區過小則會拋出例外。 對於串流的連續編碼,這種方法通常是最佳選擇。
另請參閱
適用於
GetBytes(Char[], Int32, Int32, Byte[], Int32)
當在派生類別中覆寫時,會將指定字元陣列中的一組字元編碼到指定的位元組陣列中。
public:
abstract int GetBytes(cli::array <char> ^ chars, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);
public abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex);
abstract member GetBytes : char[] * int * int * byte[] * int -> int
Public MustOverride 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 中的字元編碼)
-及-
範例
以下範例決定從字元陣列編碼三個字元所需的位元組數,編碼字元,並顯示所得位元組。
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// The characters to encode:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
// a high-surrogate value (U+D8FF)
// a low-surrogate value (U+DCFF)
char[] myChars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
// Get different encodings.
Encoding u7 = Encoding.UTF7;
Encoding u8 = Encoding.UTF8;
Encoding u16LE = Encoding.Unicode;
Encoding u16BE = Encoding.BigEndianUnicode;
Encoding u32 = Encoding.UTF32;
// Encode three characters starting at index 4, and print out the counts and the resulting bytes.
PrintCountsAndBytes( myChars, 4, 3, u7 );
PrintCountsAndBytes( myChars, 4, 3, u8 );
PrintCountsAndBytes( myChars, 4, 3, u16LE );
PrintCountsAndBytes( myChars, 4, 3, u16BE );
PrintCountsAndBytes( myChars, 4, 3, u32 );
}
public static void PrintCountsAndBytes( char[] chars, int index, int count, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( chars, index, count );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( count );
Console.Write( " {0,-3} :", iMBC );
// Encode the array of chars.
byte[] bytes = enc.GetBytes( chars, index, count );
// The following is an alternative way to encode the array of chars:
// byte[] bytes = new byte[iBC];
// enc.GetBytes( chars, index, count, bytes, bytes.GetLowerBound(0) );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintHexBytes( byte[] bytes ) {
if (( bytes == null ) || ( bytes.Length == 0 ))
{
Console.WriteLine( "<none>" );
}
else {
for ( int i = 0; i < bytes.Length; i++ )
Console.Write( "{0:X2} ", bytes[i] );
Console.WriteLine();
}
}
}
/*
This code produces the following output.
System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' The characters to encode:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
' a high-surrogate value (U+D8FF)
' a low-surrogate value (U+DCFF)
Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2), ChrW(&HD8FF), ChrW(&HDCFF) }
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode three characters starting at index 4, and print out the counts and the resulting bytes.
PrintCountsAndBytes(myChars, 4, 3, u7)
PrintCountsAndBytes(myChars, 4, 3, u8)
PrintCountsAndBytes(myChars, 4, 3, u16LE)
PrintCountsAndBytes(myChars, 4, 3, u16BE)
PrintCountsAndBytes(myChars, 4, 3, u32)
End Sub
Public Shared Sub PrintCountsAndBytes(chars() As Char, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(chars, index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode the array of chars.
Dim bytes As Byte() = enc.GetBytes(chars, index, count)
' The following is an alternative way to encode the array of chars:
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
' Dim bytes(iBC - 1) As Byte
' enc.GetBytes( chars, index, count, bytes, bytes.GetLowerBound(0) )
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
'System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
備註
要計算儲存產生的位元組所需的 GetBytes 精確陣列大小,你應該呼叫該 GetByteCount 方法。 要計算最大陣列大小,請呼叫該 GetMaxByteCount 方法。 此 GetByteCount 方法通常允許分配較少的記憶體,而執行 GetMaxByteCount 速度通常較快。
如果要轉換的資料僅以循序區塊(例如從資料流讀取的數據)形式提供,或資料量太大而需要分割成較小的區塊,則您應該使用由衍生類別的方法 Decoder 提供的 Encoder 或方法 GetDecoder 提供的 GetEncoder。
該 GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。 此 Encoding.GetBytes 方法預期離散轉換,與 Encoder.GetBytes 處理單一輸入串流多重轉換的方法不同。
支援多個版本 GetByteCount 的 and GetBytes 。 以下是使用這些方法時的一些程式設計考量:
你的應用程式可能需要將多個輸入字元編碼到一個代碼頁,並透過多次呼叫來處理這些字元。 在這種情況下,你可能需要在呼叫間維持狀態,並考慮所使用物件所持久 Encoder 化的狀態。 (例如,包含替代對的字元序列可能以高代理結尾。他們 Encoder 會記住那個高代理,這樣在下一通電話開始時就能和低代身合併。 Encoding 無法維持該狀態,因此該角色會被送往 EncoderFallback。)
如果你的應用程式處理字串輸入,你應該使用字串版本 GetBytes。
Unicode 字元緩衝區 GetBytes(Char*, Int32, Byte*, Int32) 版本允許一些快速技術,特別是在多次呼叫物件 Encoder 或插入現有緩衝區時。 不過請注意,此方法版本有時不安全,因為需要指標。
如果你的應用程式需要轉換大量資料,它應該會重複使用輸出緩衝區。 在這種情況下, GetBytes 支援位元組陣列的版本是最佳選擇。
考慮使用該 Encoder.Convert 方法而非 GetByteCount。 轉換方法會盡可能轉換大量資料,若輸出緩衝區過小則會拋出例外。 對於串流的連續編碼,這種方法通常是最佳選擇。
另請參閱
適用於
GetBytes(String, Int32, Int32, Byte[], Int32)
當在派生類別中覆寫時,會將指定字串中的一組字元編碼到指定的位元組陣列中。
public:
virtual int GetBytes(System::String ^ s, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);
public virtual int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex);
abstract member GetBytes : string * int * int * byte[] * int -> int
override this.GetBytes : string * int * int * byte[] * int -> int
Public Overridable 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。
bytes 從陣列到末端的容量 byteIndex 不足以容納產生的位元組。
後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼)
-及-
範例
以下範例決定編碼字串或字串範圍所需的位元組數,編碼字元,並顯示產生的位元組。
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// The characters to encode:
// Latin Small Letter Z (U+007A)
// Latin Small Letter A (U+0061)
// Combining Breve (U+0306)
// Latin Small Letter AE With Acute (U+01FD)
// Greek Small Letter Beta (U+03B2)
// a high-surrogate value (U+D8FF)
// a low-surrogate value (U+DCFF)
String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
// Get different encodings.
Encoding u7 = Encoding.UTF7;
Encoding u8 = Encoding.UTF8;
Encoding u16LE = Encoding.Unicode;
Encoding u16BE = Encoding.BigEndianUnicode;
Encoding u32 = Encoding.UTF32;
// Encode the entire string, and print out the counts and the resulting bytes.
Console.WriteLine( "Encoding the entire string:" );
PrintCountsAndBytes( myStr, u7 );
PrintCountsAndBytes( myStr, u8 );
PrintCountsAndBytes( myStr, u16LE );
PrintCountsAndBytes( myStr, u16BE );
PrintCountsAndBytes( myStr, u32 );
Console.WriteLine();
// Encode three characters starting at index 4, and print out the counts and the resulting bytes.
Console.WriteLine( "Encoding the characters from index 4 through 6:" );
PrintCountsAndBytes( myStr, 4, 3, u7 );
PrintCountsAndBytes( myStr, 4, 3, u8 );
PrintCountsAndBytes( myStr, 4, 3, u16LE );
PrintCountsAndBytes( myStr, 4, 3, u16BE );
PrintCountsAndBytes( myStr, 4, 3, u32 );
}
public static void PrintCountsAndBytes( String s, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( s );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( s.Length );
Console.Write( " {0,-3} :", iMBC );
// Encode the entire string.
byte[] bytes = enc.GetBytes( s );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintCountsAndBytes( String s, int index, int count, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( s.ToCharArray(), index, count );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( count );
Console.Write( " {0,-3} :", iMBC );
// Encode a range of characters in the string.
byte[] bytes = new byte[iBC];
enc.GetBytes( s, index, count, bytes, bytes.GetLowerBound(0) );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
public static void PrintHexBytes( byte[] bytes ) {
if (( bytes == null ) || ( bytes.Length == 0 ))
{
Console.WriteLine( "<none>" );
}
else {
for ( int i = 0; i < bytes.Length; i++ )
Console.Write( "{0:X2} ", bytes[i] );
Console.WriteLine();
}
}
}
/*
This code produces the following output.
Encoding the entire string:
System.Text.UTF7Encoding : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
System.Text.UTF8Encoding : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00
Encoding the characters from index 4 through 6:
System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' The characters to encode:
' Latin Small Letter Z (U+007A)
' Latin Small Letter A (U+0061)
' Combining Breve (U+0306)
' Latin Small Letter AE With Acute (U+01FD)
' Greek Small Letter Beta (U+03B2)
' a high-surrogate value (U+D8FF)
' a low-surrogate value (U+DCFF)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode the entire string, and print out the counts and the resulting bytes.
Console.WriteLine("Encoding the entire string:")
PrintCountsAndBytes(myStr, u7)
PrintCountsAndBytes(myStr, u8)
PrintCountsAndBytes(myStr, u16LE)
PrintCountsAndBytes(myStr, u16BE)
PrintCountsAndBytes(myStr, u32)
Console.WriteLine()
' Encode three characters starting at index 4, and print out the counts and the resulting bytes.
Console.WriteLine("Encoding the characters from index 4 through 6:")
PrintCountsAndBytes(myStr, 4, 3, u7)
PrintCountsAndBytes(myStr, 4, 3, u8)
PrintCountsAndBytes(myStr, 4, 3, u16LE)
PrintCountsAndBytes(myStr, 4, 3, u16BE)
PrintCountsAndBytes(myStr, 4, 3, u32)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(s.Length)
Console.Write(" {0,-3} :", iMBC)
' Encode the entire string.
Dim bytes As Byte() = enc.GetBytes(s)
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s.ToCharArray(), index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode a range of characters in the string.
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
Dim bytes(iBC - 1) As Byte
enc.GetBytes(s, index, count, bytes, bytes.GetLowerBound(0))
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'Encoding the entire string:
'System.Text.UTF7Encoding : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
'System.Text.UTF8Encoding : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00
'
'Encoding the characters from index 4 through 6:
'System.Text.UTF7Encoding : 10 11 :2B 41 37 4C 59 2F 39 7A 2F 2D
'System.Text.UTF8Encoding : 6 12 :CE B2 F1 8F B3 BF
'System.Text.UnicodeEncoding : 6 8 :B2 03 FF D8 FF DC
'System.Text.UnicodeEncoding : 6 8 :03 B2 D8 FF DC FF
'System.Text.UTF32Encoding : 8 16 :B2 03 00 00 FF FC 04 00
備註
要計算儲存產生的位元組所需的 GetBytes 精確陣列大小,你應該呼叫該 GetByteCount 方法。 要計算最大陣列大小,請呼叫該 GetMaxByteCount 方法。 此 GetByteCount 方法通常允許分配較少的記憶體,而執行 GetMaxByteCount 速度通常較快。
如果要轉換的資料僅以循序區塊(例如從資料流讀取的數據)形式提供,或資料量太大而需要分割成較小的區塊,則您應該使用由衍生類別的方法 Decoder 提供的 Encoder 或方法 GetDecoder 提供的 GetEncoder。
該 GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。 此 Encoding.GetBytes 方法預期離散轉換,與 Encoder.GetBytes 處理單一輸入串流多重轉換的方法不同。
支援多個版本 GetByteCount 的 and GetBytes 。 以下是使用這些方法時的一些程式設計考量:
你的應用程式可能需要將多個輸入字元編碼到一個代碼頁,並透過多次呼叫來處理這些字元。 在這種情況下,你可能需要在呼叫間維持狀態,並考慮所使用物件所持久 Encoder 化的狀態。 (例如,包含替代對的字元序列可能以高代理結尾。他們 Encoder 會記住那個高代理,這樣在下一通電話開始時就能和低代身合併。 Encoding 無法維持該狀態,因此該角色會被送往 EncoderFallback。)
如果你的應用程式處理字串輸入,你應該使用字串版本 GetBytes。
Unicode 字元緩衝區 GetBytes(Char*, Int32, Byte*, Int32) 版本允許一些快速技術,特別是在多次呼叫物件 Encoder 或插入現有緩衝區時。 不過請注意,此方法版本有時不安全,因為需要指標。
如果你的應用程式需要轉換大量資料,它應該會重複使用輸出緩衝區。 在這種情況下, GetBytes 支援位元組陣列的版本是最佳選擇。
考慮使用該 Encoder.Convert 方法而非 GetByteCount。 轉換方法會盡可能轉換大量資料,若輸出緩衝區過小則會拋出例外。 對於串流的連續編碼,這種方法通常是最佳選擇。