UnicodeEncoding.GetEncoder Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Obtains an encoder that converts a sequence of Unicode characters into a UTF-16 encoded sequence of bytes.
public:
override System::Text::Encoder ^ GetEncoder();
public override System.Text.Encoder GetEncoder ();
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Text.Encoder GetEncoder ();
override this.GetEncoder : unit -> System.Text.Encoder
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetEncoder : unit -> System.Text.Encoder
Public Overrides Function GetEncoder () As Encoder
A Encoder object that converts a sequence of Unicode characters into a UTF-16 encoded sequence of bytes.
- Attributes
The following example uses an encoder and a decoder to encode a string into an array of bytes, and then decode the bytes into an array of characters.
using namespace System;
using namespace System::Text;
int main()
{
// Get an encoder and a decoder from UnicodeEncoding.
UnicodeEncoding^ u16 = gcnew UnicodeEncoding( false,true,true );
Encoder^ myEnc = u16->GetEncoder();
Decoder^ myDec = u16->GetDecoder();
// 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)
array<Char>^myChars = gcnew array<Char>(5){
L'z',L'a',L'\u0306',L'\u01FD',L'\u03B2'
};
Console::Write( "The original characters : " );
Console::WriteLine( myChars );
// Encode the character array.
int iBC = myEnc->GetByteCount( myChars, 0, myChars->Length, true );
array<Byte>^myBytes = gcnew array<Byte>(iBC);
myEnc->GetBytes( myChars, 0, myChars->Length, myBytes, 0, true );
// Print the resulting bytes.
Console::Write( "Using the encoder : " );
for ( int i = 0; i < myBytes->Length; i++ )
Console::Write( "{0:X2} ", myBytes[ i ] );
Console::WriteLine();
// Decode the byte array back into an array of characters.
int iCC = myDec->GetCharCount( myBytes, 0, myBytes->Length, true );
array<Char>^myDecodedChars = gcnew array<Char>(iCC);
myDec->GetChars( myBytes, 0, myBytes->Length, myDecodedChars, 0, true );
// Print the resulting characters.
Console::Write( "Using the decoder : " );
Console::WriteLine( myDecodedChars );
}
/*
This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
The original characters : za??�
Using the encoder : 7A 00 61 00 06 03 FD 01 B2 03
Using the decoder : za??�
*/
using System;
using System.Text;
public class SamplesUnicodeEncoding {
public static void Main() {
// Get an encoder and a decoder from UnicodeEncoding.
UnicodeEncoding u16 = new UnicodeEncoding( false, true, true );
Encoder myEnc = u16.GetEncoder();
Decoder myDec = u16.GetDecoder();
// 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)
char[] myChars = new char[5] { 'z', 'a', '\u0306', '\u01FD', '\u03B2' };
Console.Write( "The original characters : " );
Console.WriteLine( myChars );
// Encode the character array.
int iBC = myEnc.GetByteCount( myChars, 0, myChars.Length, true );
byte[] myBytes = new byte[iBC];
myEnc.GetBytes( myChars, 0, myChars.Length, myBytes, 0, true );
// Print the resulting bytes.
Console.Write( "Using the encoder : " );
for ( int i = 0; i < myBytes.Length; i++ )
Console.Write( "{0:X2} ", myBytes[i] );
Console.WriteLine();
// Decode the byte array back into an array of characters.
int iCC = myDec.GetCharCount( myBytes, 0, myBytes.Length, true );
char[] myDecodedChars = new char[iCC];
myDec.GetChars( myBytes, 0, myBytes.Length, myDecodedChars, 0, true );
// Print the resulting characters.
Console.Write( "Using the decoder : " );
Console.WriteLine( myDecodedChars );
}
}
/*
This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
The original characters : za??ß
Using the encoder : 7A 00 61 00 06 03 FD 01 B2 03
Using the decoder : za??ß
*/
Imports System.Text
Public Class SamplesUnicodeEncoding
Public Shared Sub Main()
' Get an encoder and a decoder from UnicodeEncoding.
Dim u16 As New UnicodeEncoding(False, True, True)
Dim myEnc As Encoder = u16.GetEncoder()
Dim myDec As Decoder = u16.GetDecoder()
' 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)
Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2)}
Console.Write("The original characters : ")
Console.WriteLine(myChars)
' Encode the character array.
Dim iBC As Integer = myEnc.GetByteCount(myChars, 0, myChars.Length, True)
' NOTE: In Visual Basic, arrays contain one extra element by default.
' The following line creates an array with the exact number of elements required.
Dim myBytes(iBC - 1) As Byte
myEnc.GetBytes(myChars, 0, myChars.Length, myBytes, 0, True)
' Print the resulting bytes.
Console.Write("Using the encoder : ")
Dim i As Integer
For i = 0 To myBytes.Length - 1
Console.Write("{0:X2} ", myBytes(i))
Next i
Console.WriteLine()
' Decode the byte array back into an array of characters.
Dim iCC As Integer = myDec.GetCharCount(myBytes, 0, myBytes.Length, True)
' NOTE: In Visual Basic, arrays contain one extra element by default.
' The following line creates an array with the exact number of elements required.
Dim myDecodedChars(iCC - 1) As Char
myDec.GetChars(myBytes, 0, myBytes.Length, myDecodedChars, 0, True)
' Print the resulting characters.
Console.Write("Using the decoder : ")
Console.WriteLine(myDecodedChars)
End Sub
End Class
'This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
'
'The original characters : za??ß
'Using the encoder : 7A 00 61 00 06 03 FD 01 B2 03
'Using the decoder : za??ß
The Encoder.GetBytes method converts sequential blocks of characters into sequential blocks of bytes in a manner similar to the GetBytes method of this class. However, a Encoder object maintains state information between calls so that it can correctly encode character sequences that span blocks. The Encoder object also preserves trailing characters at the end of data blocks and uses the trailing characters in the next encoding operation. For example, a data block might end with an unmatched high surrogate, and the matching low surrogate might be in the next data block. Therefore, GetDecoder and GetEncoder are useful for network transmission and file operations, because those operations often deal with blocks of data instead of a complete data stream.
If error detection is enabled, that is, the throwOnInvalidBytes
parameter of the constructor is set to true
, error detection is also enabled in the Encoder object returned by this method. If error detection is enabled and an invalid sequence is encountered, the state of the encoder is undefined and processing must stop.
Produit | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :