Encoding.GetBytes 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,将一组字符编码为一个字节序列。
重载
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[])
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
在派生类中重写时,将指定字符数组中的所有字符编码为一个字节序列。
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 namespace System;
using namespace System::Text;
void PrintCountsAndBytes( array<Char>^chars, Encoding^ enc );
void PrintHexBytes( array<Byte>^bytes );
int 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)
array<Char>^myChars = gcnew array<Char>{
L'z','a',L'\u0306',L'\u01FD',L'\u03B2',L'\xD8FF',L'\xDCFF'
};
// 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 );
}
void PrintCountsAndBytes( array<Char>^chars, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<Byte>^bytes = enc->GetBytes( chars );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
void PrintHexBytes( array<Byte>^bytes )
{
if ( (bytes == nullptr) || (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
*/
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
注解
如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量很大以致需要分为更小块,则应使用 DecoderEncoderGetDecoder 派生类的方法或方法提供的或 GetEncoder 。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。 GetBytes方法需要离散转换,这与方法不同, Encoder.GetBytes 后者处理单个输入流上的多个转换。
支持和的多个版本 GetByteCountGetBytes 。 下面是有关使用这些方法的一些编程注意事项:
您的应用程序可能需要将许多输入字符编码为代码页,并使用多个调用处理这些字符。 在这种情况下,您可能需要维护两次调用之间的状态,同时考虑所使用的对象保留的状态 Encoder 。 (例如,包含代理项对的字符序列可能以高代理项结尾。 Encoder 将记住高代理项,以便在以下调用开始时将其与低代理项组合在一起。 Encoding 将无法保持状态,因此字符将发送到 EncoderFallback.)
如果你的应用程序处理字符串输入,则应调用该方法的字符串版本 GetBytes 。
的 Unicode 字符缓冲区版本 GetBytes(Char*, Int32, Byte*, Int32) 允许一些快速的技巧,尤其是使用对象的多个调用 Encoder 或插入现有缓冲区。 但请记住,此方法版本有时不安全,因为指针是必需的。
如果你的应用程序必须转换大量数据,则应重新使用输出缓冲区。 在这种情况下, GetBytes 支持字节数组的版本是最佳选择。
请考虑使用 Encoder.Convert 方法而不是 GetByteCount 。 转换方法可转换尽可能多的数据,如果输出缓冲区太小,则会引发异常。 对于流的连续编码,此方法通常是最佳选择。
另请参阅
适用于
GetBytes(String)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
在派生类中重写时,将指定字符串中的所有字符编码为一个字节序列。
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 namespace System;
using namespace System::Text;
void PrintCountsAndBytes( String^ s, Encoding^ enc );
void PrintCountsAndBytes( String^ s, int index, int count, Encoding^ enc );
void PrintHexBytes( array<Byte>^bytes );
int 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 = L"za\u0306\u01FD\u03B2\xD8FF\xDCFF";
// 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 );
}
void PrintCountsAndBytes( String^ s, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<Byte>^bytes = enc->GetBytes( s );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
void PrintCountsAndBytes( String^ s, int index, int count, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<Byte>^bytes = gcnew array<Byte>(iBC);
enc->GetBytes( s, index, count, bytes, bytes->GetLowerBound( 0 ) );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
void PrintHexBytes( array<Byte>^bytes )
{
if ( (bytes == nullptr) || (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
*/
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
注解
如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量很大以致需要分为更小块,则应使用 DecoderEncoderGetDecoder 派生类的方法或方法提供的或 GetEncoder 。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。 Encoding.GetBytes方法需要离散转换,这与方法不同, Encoder.GetBytes 后者处理单个输入流上的多个转换。
支持和的多个版本 GetByteCountGetBytes 。 下面是有关使用这些方法的一些编程注意事项:
您的应用程序可能需要将许多输入字符编码为代码页,并使用多个调用处理这些字符。 在这种情况下,您可能需要维护两次调用之间的状态,同时考虑所使用的对象保留的状态 Encoder 。 (例如,包含代理项对的字符序列可能以高代理项结尾。 Encoder 将记住高代理项,以便在以下调用开始时将其与低代理项组合在一起。 Encoding 将无法保持状态,因此字符将发送到 EncoderFallback.)
如果你的应用程序处理字符串输入,则应使用的字符串版本 GetBytes 。
的 Unicode 字符缓冲区版本 GetBytes(Char*, Int32, Byte*, Int32) 允许一些快速的技巧,尤其是使用对象的多个调用 Encoder 或插入现有缓冲区。 但请记住,此方法版本有时不安全,因为指针是必需的。
如果你的应用程序必须转换大量数据,则应重新使用输出缓冲区。 在这种情况下, GetBytes 支持字节数组的版本是最佳选择。
请考虑使用 Encoder.Convert 方法而不是 GetByteCount 。 转换方法可转换尽可能多的数据,如果输出缓冲区太小,则会引发异常。 对于流的连续编码,此方法通常是最佳选择。
另请参阅
适用于
GetBytes(ReadOnlySpan<Char>, Span<Byte>)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
在派生类中重写时,将指定只读范围中的一组字符编码为字节范围。
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>
包含要编码的字符集的范围。
返回
已编码的字节数。
注解
如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量很大以致需要分为更小块,则应使用 DecoderEncoderGetDecoder 派生类的方法或方法提供的或 GetEncoder 。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。 Encoding.GetBytes方法需要离散转换,这与方法不同, Encoder.GetBytes 后者处理单个输入流上的多个转换。
支持和的多个版本 GetByteCountGetBytes 。 下面是有关使用这些方法的一些编程注意事项:
您的应用程序可能需要将许多输入字符编码为代码页,并使用多个调用处理这些字符。 在这种情况下,您可能需要维护两次调用之间的状态,同时考虑所使用的对象保留的状态 Encoder 。 (例如,包含代理项对的字符序列可能以高代理项结尾。 Encoder 将记住高代理项,以便在以下调用开始时将其与低代理项组合在一起。 Encoding 将无法保持状态,因此字符将发送到 EncoderFallback.)
如果你的应用程序处理字符串输入,则应使用的字符串版本 GetBytes 。
如果你的应用程序必须转换大量数据,则应重新使用输出缓冲区。 在这种情况下, GetBytes 支持字节数组的版本是最佳选择。
请考虑使用 Encoder.Convert 方法而不是 GetByteCount 。 转换方法可转换尽可能多的数据,如果输出缓冲区太小,则会引发异常。 对于流的连续编码,此方法通常是最佳选择。
适用于
GetBytes(Char[], Int32, Int32)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
在派生类中重写时,将指定字符数组中的一组字符编码为一个字节序列。
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 namespace System;
using namespace System::Text;
void PrintCountsAndBytes( array<Char>^chars, int index, int count, Encoding^ enc );
void PrintHexBytes( array<Byte>^bytes );
int 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)
array<Char>^myChars = gcnew array<Char>{
L'z',L'a',L'\u0306',L'\u01FD',L'\u03B2',L'\xD8FF',L'\xDCFF'
};
// 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 );
}
void PrintCountsAndBytes( array<Char>^chars, int index, int count, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<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 );
}
void PrintHexBytes( array<Byte>^bytes )
{
if ( (bytes == nullptr) || (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
*/
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
注解
如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量很大以致需要分为更小块,则应使用 DecoderEncoderGetDecoder 派生类的方法或方法提供的或 GetEncoder 。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。 Encoding.GetBytes方法需要离散转换,这与方法不同, Encoder.GetBytes 后者处理单个输入流上的多个转换。
支持和的多个版本 GetByteCountGetBytes 。 下面是有关使用这些方法的一些编程注意事项:
您的应用程序可能需要将许多输入字符编码为代码页,并使用多个调用处理这些字符。 在这种情况下,您可能需要维护两次调用之间的状态,同时考虑所使用的对象保留的状态 Encoder 。 (例如,包含代理项对的字符序列可能以高代理项结尾。 Encoder 将记住高代理项,以便它可以在后续调用开始时与低代理项结合使用。 Encoding 将无法保持 状态,因此字符将发送到 EncoderFallback.)
如果你的应用程序处理字符串输入,则应使用的字符串版本 GetBytes 。
的 Unicode 字符缓冲区版本 GetBytes(Char*, Int32, Byte*, Int32) 允许一些快速的技巧,尤其是使用对象的多个调用 Encoder 或插入现有缓冲区。 但请记住,此方法版本有时不安全,因为指针是必需的。
如果你的应用程序必须转换大量数据,则应重新使用输出缓冲区。 在这种情况下, GetBytes 支持字节数组的版本是最佳选择。
请考虑使用 Encoder.Convert 方法而不是 GetByteCount 。 转换方法可转换尽可能多的数据,如果输出缓冲区太小,则会引发异常。 对于流的连续编码,此方法通常是最佳选择。
另请参阅
适用于
GetBytes(String, Int32, Int32)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
在派生类中重写时,从指定的 index
开始,将指定字符串中由 count
指定的字符数编码为字节数组。
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 namespace System;
using namespace System::Text;
void PrintCountsAndBytes( String^ s, Encoding^ enc );
void PrintCountsAndBytes( String^ s, int index, int count, Encoding^ enc );
void PrintHexBytes( array<Byte>^bytes );
int 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 = L"za\u0306\u01FD\u03B2\xD8FF\xDCFF";
// 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 );
}
void PrintCountsAndBytes( String^ s, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<Byte>^bytes = enc->GetBytes( s );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
void PrintCountsAndBytes( String^ s, int index, int count, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<Byte>^bytes = gcnew array<Byte>(iBC);
enc->GetBytes( s, index, count, bytes, bytes->GetLowerBound( 0 ) );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
void PrintHexBytes( array<Byte>^bytes )
{
if ( (bytes == nullptr) || (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
*/
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
注解
如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量很大以致需要分为更小块,则应使用 DecoderEncoderGetDecoder 派生类的方法或方法提供的或 GetEncoder 。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。 Encoding.GetBytes方法需要离散转换,这与方法不同, Encoder.GetBytes 后者处理单个输入流上的多个转换。
支持和的多个版本 GetByteCountGetBytes 。 下面是有关使用这些方法的一些编程注意事项:
您的应用程序可能需要将许多输入字符编码为代码页,并使用多个调用处理这些字符。 在这种情况下,您可能需要维护两次调用之间的状态,同时考虑所使用的对象保留的状态 Encoder 。 (例如,包含代理项对的字符序列可能以高代理项结尾。 Encoder 将记住高代理项,以便它可以在后续调用开始时与低代理项结合使用。 Encoding 将无法保持 状态,因此字符将发送到 EncoderFallback.)
如果你的应用程序处理字符串输入,则应使用的字符串版本 GetBytes 。
的 Unicode 字符缓冲区版本 GetBytes(Char*, Int32, Byte*, Int32) 允许一些快速的技巧,尤其是使用对象的多个调用 Encoder 或插入现有缓冲区。 但请记住,此方法版本有时不安全,因为指针是必需的。
如果你的应用程序必须转换大量数据,则应重新使用输出缓冲区。 在这种情况下, GetBytes 支持字节数组的版本是最佳选择。
请考虑使用 Encoder.Convert 方法而不是 GetByteCount 。 转换方法可转换尽可能多的数据,如果输出缓冲区太小,则会引发异常。 对于流的连续编码,此方法通常是最佳选择。
适用于
GetBytes(Char*, Int32, Byte*, Int32)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
重要
此 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)]
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)>]
[<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)>]
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
参数
- chars
- Char*
指向第一个要编码的字符的指针。
- charCount
- Int32
要编码的字符的数目。
- bytes
- Byte*
一个指针,指向开始写入所产生的字节序列的位置。
- byteCount
- Int32
最多写入的字节数。
返回
在由 bytes
参数指示的位置处写入的实际字节数。
- 属性
例外
charCount
或 byteCount
小于零。
byteCount
少于所产生的字节数。
发生回退(有关详细信息,请参阅采用 .NET 的字符编码)
-和-
注解
若要计算 GetBytes 存储生成的字节所需的确切数组大小,请调用 GetByteCount 方法。 若要计算最大数组大小,请调用 GetMaxByteCount 方法。 GetByteCount方法通常允许分配较少的内存,而 GetMaxByteCount 方法的执行速度通常更快。
如果要转换的数据仅在 (如从流中读取的数据) 的顺序块中可用,或者如果数据量很大,它需要划分为较小的块,则应使用Decoder或Encoder提供对象GetDecoder或GetEncoder方法,分别的派生类。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。 GetBytes方法需要离散转换,这与方法不同, Encoder.GetBytes 后者处理单个输入流上的多个转换。
支持和的多个版本 GetByteCountGetBytes 。 下面是有关使用这些方法的一些编程注意事项:
您的应用程序可能需要将许多输入字符编码为代码页,并使用多个调用处理这些字符。 在这种情况下,您可能需要维护两次调用之间的状态,同时考虑所使用的对象保留的状态 Encoder 。 (例如,包含代理项对的字符序列可能以高代理项结尾。 Encoder 将记住高代理项,以便它可以在后续调用开始时与低代理项结合使用。 Encoding 将无法保持 状态,因此字符将发送到 EncoderFallback.)
如果你的应用程序处理字符串输入,则应使用的字符串版本 GetBytes 。
的 Unicode 字符缓冲区版本 GetBytes(Char*, Int32, Byte*, Int32) 允许一些快速的技巧,尤其是使用对象的多个调用 Encoder 或插入现有缓冲区。 但请记住,此方法版本有时不安全,因为指针是必需的。
如果你的应用程序必须转换大量数据,则应重新使用输出缓冲区。 在这种情况下, GetBytes 支持字节数组的版本是最佳选择。
请考虑使用 Encoder.Convert 方法而不是 GetByteCount 。 转换方法可转换尽可能多的数据,如果输出缓冲区太小,则会引发异常。 对于流的连续编码,此方法通常是最佳选择。
另请参阅
适用于
GetBytes(Char[], Int32, Int32, Byte[], Int32)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
在派生类中重写时,将指定字符数组中的一组字符编码为指定的字节数组。
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 namespace System;
using namespace System::Text;
void PrintCountsAndBytes( array<Char>^chars, int index, int count, Encoding^ enc );
void PrintHexBytes( array<Byte>^bytes );
int 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)
array<Char>^myChars = gcnew array<Char>{
L'z',L'a',L'\u0306',L'\u01FD',L'\u03B2',L'\xD8FF',L'\xDCFF'
};
// 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 );
}
void PrintCountsAndBytes( array<Char>^chars, int index, int count, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<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 );
}
void PrintHexBytes( array<Byte>^bytes )
{
if ( (bytes == nullptr) || (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
*/
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 方法的执行速度通常更快。
如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量很大以致需要分为更小块,则应使用 DecoderEncoderGetDecoder 派生类的方法或方法提供的或 GetEncoder 。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。 Encoding.GetBytes方法需要离散转换,这与方法不同, Encoder.GetBytes 后者处理单个输入流上的多个转换。
支持和的多个版本 GetByteCountGetBytes 。 下面是有关使用这些方法的一些编程注意事项:
您的应用程序可能需要将许多输入字符编码为代码页,并使用多个调用处理这些字符。 在这种情况下,您可能需要维护两次调用之间的状态,同时考虑所使用的对象保留的状态 Encoder 。 (例如,包含代理项对的字符序列可能以高代理项结尾。 Encoder 将记住高代理项,以便在以下调用开始时将其与低代理项组合在一起。 Encoding 将无法保持状态,因此字符将发送到 EncoderFallback.)
如果你的应用程序处理字符串输入,则应使用的字符串版本 GetBytes 。
的 Unicode 字符缓冲区版本 GetBytes(Char*, Int32, Byte*, Int32) 允许一些快速的技巧,尤其是使用对象的多个调用 Encoder 或插入现有缓冲区。 但请记住,此方法版本有时不安全,因为指针是必需的。
如果你的应用程序必须转换大量数据,则应重新使用输出缓冲区。 在这种情况下, GetBytes 支持字节数组的版本是最佳选择。
请考虑使用 Encoder.Convert 方法而不是 GetByteCount 。 转换方法可转换尽可能多的数据,如果输出缓冲区太小,则会引发异常。 对于流的连续编码,此方法通常是最佳选择。
另请参阅
适用于
GetBytes(String, Int32, Int32, Byte[], Int32)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
在派生类中重写时,将指定字符串中的一组字符编码为指定的字节数组。
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
不表示 chars
中的有效范围。
或
byteIndex
不是 bytes
中的有效索引。
bytes
中从 byteIndex
到数组结尾没有足够的容量来容纳所产生的字节。
发生回退(有关详细信息,请参阅采用 .NET 的字符编码)
-和-
示例
下面的示例确定了对字符串中的字符串或范围进行编码、对字符进行编码并显示生成的字节所需的字节数。
using namespace System;
using namespace System::Text;
void PrintCountsAndBytes( String^ s, Encoding^ enc );
void PrintCountsAndBytes( String^ s, int index, int count, Encoding^ enc );
void PrintHexBytes( array<Byte>^bytes );
int 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 = L"za\u0306\u01FD\u03B2\xD8FF\xDCFF";
// 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 );
}
void PrintCountsAndBytes( String^ s, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<Byte>^bytes = enc->GetBytes( s );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
void PrintCountsAndBytes( String^ s, int index, int count, Encoding^ enc )
{
// Display the name of the encoding used.
Console::Write( "{0,-30} :", enc );
// 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.
array<Byte>^bytes = gcnew array<Byte>(iBC);
enc->GetBytes( s, index, count, bytes, bytes->GetLowerBound( 0 ) );
// Display all the encoded bytes.
PrintHexBytes( bytes );
}
void PrintHexBytes( array<Byte>^bytes )
{
if ( (bytes == nullptr) || (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
*/
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 方法的执行速度通常更快。
如果要转换的数据仅在顺序块(如从流中读取的数据)中可用,或者如果数据量很大以致需要分为更小块,则应使用 DecoderEncoderGetDecoder 派生类的方法或方法提供的或 GetEncoder 。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。 Encoding.GetBytes方法需要离散转换,这与方法不同, Encoder.GetBytes 后者处理单个输入流上的多个转换。
支持和的多个版本 GetByteCountGetBytes 。 下面是有关使用这些方法的一些编程注意事项:
您的应用程序可能需要将许多输入字符编码为代码页,并使用多个调用处理这些字符。 在这种情况下,您可能需要维护两次调用之间的状态,同时考虑所使用的对象保留的状态 Encoder 。 (例如,包含代理项对的字符序列可能以高代理项结尾。 Encoder 将记住高代理项,以便在以下调用开始时将其与低代理项组合在一起。 Encoding 将无法保持状态,因此字符将发送到 EncoderFallback.)
如果你的应用程序处理字符串输入,则应使用的字符串版本 GetBytes 。
的 Unicode 字符缓冲区版本 GetBytes(Char*, Int32, Byte*, Int32) 允许一些快速的技巧,尤其是使用对象的多个调用 Encoder 或插入现有缓冲区。 但请记住,此方法版本有时不安全,因为指针是必需的。
如果你的应用程序必须转换大量数据,则应重新使用输出缓冲区。 在这种情况下, GetBytes 支持字节数组的版本是最佳选择。
请考虑使用 Encoder.Convert 方法而不是 GetByteCount 。 转换方法可转换尽可能多的数据,如果输出缓冲区太小,则会引发异常。 对于流的连续编码,此方法通常是最佳选择。