UTF7Encoding.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-7 encoded sequence of bytes.
public:
override System::Text::Encoder ^ GetEncoder();
public override System.Text.Encoder GetEncoder ();
override this.GetEncoder : unit -> System.Text.Encoder
Public Overrides Function GetEncoder () As Encoder
A Encoder that converts a sequence of Unicode characters into a UTF-7 encoded sequence of bytes.
The following code example demonstrates how to use the GetEncoder method to obtain an encoder to convert a sequence of characters into a UTF-7 encoded sequence of bytes.
using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
array<Char>^chars = {'a','b','c',L'\u0300',L'\ua0a0'};
array<Byte>^bytes;
Encoder^ utf7Encoder = Encoding::UTF7->GetEncoder();
int byteCount = utf7Encoder->GetByteCount( chars, 2, 3, true );
bytes = gcnew array<Byte>(byteCount);
int bytesEncodedCount = utf7Encoder->GetBytes( chars, 2, 3, bytes, 0, true );
Console::WriteLine( "{0} bytes used to encode characters.", bytesEncodedCount );
Console::Write( "Encoded bytes: " );
IEnumerator^ myEnum = bytes->GetEnumerator();
while ( myEnum->MoveNext() )
{
Byte b = safe_cast<Byte>(myEnum->Current);
Console::Write( "[{0}]", b );
}
Console::WriteLine();
}
using System;
using System.Text;
class UTF7EncodingExample {
public static void Main() {
Char[] chars = new Char[] {'a', 'b', 'c', '\u0300', '\ua0a0'};
Byte[] bytes;
Encoder utf7Encoder = Encoding.UTF7.GetEncoder();
int byteCount = utf7Encoder.GetByteCount(chars, 2, 3, true);
bytes = new Byte[byteCount];
int bytesEncodedCount = utf7Encoder.GetBytes(chars, 2, 3, bytes, 0, true);
Console.WriteLine(
"{0} bytes used to encode characters.", bytesEncodedCount
);
Console.Write("Encoded bytes: ");
foreach (Byte b in bytes) {
Console.Write("[{0}]", b);
}
Console.WriteLine();
}
}
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class UTF7EncodingExample
Public Shared Sub Main()
'Characters:
' ChrW(97) = a
' ChrW(98) = b
' ChrW(99) = c
' ChrW(768) = `
' ChrW(41120) = valid unicode code point, but not a character
Dim chars() As Char = {ChrW(97), ChrW(98), ChrW(99), ChrW(768), ChrW(41120)}
Dim bytes() As Byte
Dim utf7Encoder As Encoder = Encoding.UTF7.GetEncoder()
Dim byteCount As Integer = utf7Encoder.GetByteCount(chars, 2, 3, True)
bytes = New Byte(byteCount - 1) {}
Dim bytesEncodedCount As Integer = utf7Encoder.GetBytes(chars, 2, 3, bytes, 0, True)
Console.WriteLine("{0} bytes used to encode characters.", bytesEncodedCount)
Console.Write("Encoded bytes: ")
Dim b As Byte
For Each b In bytes
Console.Write("[{0}]", b)
Next b
Console.WriteLine()
End Sub
End Class
The Decoder.GetChars method converts sequential blocks of bytes into sequential blocks of characters, in a manner similar to the GetChars method. However, a Decoder maintains state information between calls so it can correctly decode byte sequences that span blocks. The Decoder also preserves trailing bytes at the end of data blocks and uses the trailing bytes in the next decoding operation. 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.
Product | 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 | 1.1, 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.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: