Encoder.Convert メソッド

定義

バッファーまたは配列に格納されている Unicode 文字を、エンコード済みバイト シーケンスに変換します。

オーバーロード

Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean)

Unicode 文字のスパンをエンコードされたバイト シーケンスに変換し、結果を別のバッファーに格納します。

Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean)

バッファーに格納されている Unicode 文字をエンコード済みバイト シーケンスに変換し、その結果を別のバッファーに格納します。

Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean)

Unicode 文字の配列をエンコード済みバイト シーケンスに変換し、その結果をバイト配列に格納します。

Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean)

Unicode 文字のスパンをエンコードされたバイト シーケンスに変換し、結果を別のバッファーに格納します。

public:
 virtual void Convert(ReadOnlySpan<char> chars, Span<System::Byte> bytes, bool flush, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] bool % completed);
public virtual void Convert (ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
abstract member Convert : ReadOnlySpan<char> * Span<byte> * bool * int * int * bool -> unit
override this.Convert : ReadOnlySpan<char> * Span<byte> * bool * int * int * bool -> unit
Public Overridable Sub Convert (chars As ReadOnlySpan(Of Char), bytes As Span(Of Byte), flush As Boolean, ByRef charsUsed As Integer, ByRef bytesUsed As Integer, ByRef completed As Boolean)

パラメーター

chars
ReadOnlySpan<Char>

変換する UTF-16 エンコードの文字スパン。

bytes
Span<Byte>

変換されたバイトを格納するスパン バッファー。

flush
Boolean

これ以上データの変換を行わないことを示す場合は true。それ以外の場合は false

charsUsed
Int32

このメソッドから制御が戻るときに、chars で変換に使用された文字数を格納します。 このパラメーターは初期化せずに渡されます。

bytesUsed
Int32

このメソッドから制御が戻るときに、変換に使用されたバイト数を格納します。 このパラメーターは初期化せずに渡されます。

completed
Boolean

このメソッドから制御が戻るときに、chars のすべての文字が変換された場合は true。それ以外の場合は false。 このパラメーターは初期化せずに渡されます。

注釈

オブジェクトは、 の Encoder 呼び出し間に状態を保存します Convert 。 アプリケーションがデータストリームを使用して完了したら、 パラメーターを に設定して、状態情報がフラッシュ flush true される必要があります。 この設定では、エンコーダーはデータ ブロックの末尾にある無効なバイトを無視し、内部バッファーをクリアします。 サロゲート ペアの高サロゲートなど、論理ユニットの一部である残りの処理されたデータは、現在のフォールバック設定に従って変換されます。

メソッドは、ファイルやストリームから読み取ったデータなど、任意の量の入力をエンコードするためにループで使用 Convert するように設計されています。 これは、エンコード操作の出力を固定サイズのバッファーに格納します。 GetBytes は、出力バッファーのサイズが十分ではない場合は例外をスローしますが、可能な限り多くの領域を埋め、読み取った文字と書き込まれたバイト Convert を返します。 その他のコメント Encoding.GetBytes については、トピックも参照してください。

output パラメーターは、入力バッファー内のすべてのデータが変換され、出力バッファーに格納 completed されたかどうかを示します。 パラメーターで指定された文字数を、パラメーターで指定されたバイト数を超えないと変換できない場合、このパラメーターは false charCount に設定 byteCount されます。 このような場合、アプリケーションは出力バッファーの内容を使用するか、新しい出力バッファーを提供し、パラメーターで指定された文字数でパラメーターをインクリメントしてから、 メソッドを再度呼び出して残りの入力を処理する必要があります。 chars charsUsed Convert

パラメーターと 'char' スパンの長さが等しい場合でも、 パラメーターを completed false charsUsed に設定することもできます。 この状況は、バッファーに格納されていないデータがまだ オブジェクト Encoder に存在する場合に発生 chars します。

適用対象

Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean)

重要

この API は CLS 準拠ではありません。

CLS 準拠の代替
System.Text.Encoder.Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean)

バッファーに格納されている Unicode 文字をエンコード済みバイト シーケンスに変換し、その結果を別のバッファーに格納します。

public:
 virtual void Convert(char* chars, int charCount, System::Byte* bytes, int byteCount, bool flush, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] bool % completed);
[System.CLSCompliant(false)]
public virtual void Convert (char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
public virtual void Convert (char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Convert (char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual void Convert (char* chars, int charCount, byte* bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
[<System.CLSCompliant(false)>]
abstract member Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit
abstract member Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Security.SecurityCritical>]
abstract member Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<char> * int * nativeptr<byte> * int * bool * int * int * bool -> unit

パラメーター

chars
Char*

UTF-16 でエンコードされた変換対象の文字の文字列のアドレス。

charCount
Int32

変換する chars 内の文字の数。

bytes
Byte*

変換後のバイトを格納するバッファーのアドレス。

byteCount
Int32

変換に使用する bytes 内の最大バイト数。

flush
Boolean

これ以上データの変換を行わないことを示す場合は true。それ以外の場合は false

charsUsed
Int32

このメソッドから制御が戻るときに、chars で変換に使用された文字数を格納します。 このパラメーターは初期化せずに渡されます。

bytesUsed
Int32

このメソッドから制御が戻るときに、変換に使用されたバイト数を格納します。 このパラメーターは初期化せずに渡されます。

completed
Boolean

このメソッドから制御が戻るときに、charCount で指定したすべての文字が変換された場合は true。それ以外の場合は false。 このパラメーターは初期化せずに渡されます。

属性

例外

chars または bytesnull (Nothing) です。

charCount または byteCount が 0 未満です。

出力バッファーが小さすぎて、変換した入力を格納できません。 出力バッファーは、GetByteCount メソッドで示されるサイズ以上である必要があります。

フォールバックが発生しました (詳細については「.NET での文字エンコード」を参照)

および

FallbackEncoderExceptionFallback に設定されます。

注釈

オブジェクトは、 の Encoder 呼び出し間に状態を保存します Convert 。 アプリケーションがデータストリームを使用して完了したら、 パラメーターを に設定して、状態情報がフラッシュ flush true される必要があります。 この設定では、エンコーダーはデータ ブロックの末尾にある無効なバイトを無視し、内部バッファーをクリアします。 サロゲート ペアの高サロゲートなど、論理ユニットの一部である残りの処理されたデータは、現在のフォールバック設定に従って変換されます。

メソッドは、ファイルやストリームから読み取ったデータなど、任意の量の入力をエンコードするためにループで使用 Convert するように設計されています。 これは、エンコード操作の出力を固定サイズのバッファーに格納します。 GetBytes は、出力バッファーのサイズが十分ではない場合は例外をスローしますが、可能な限り多くの領域を埋め、読み取った文字と書き込まれたバイト Convert を返します。 その他のコメント Encoding.GetBytes については、トピックも参照してください。

output パラメーターは、入力バッファー内のすべてのデータが変換され、出力バッファーに格納 completed されたかどうかを示します。 パラメーターで指定された文字数を、パラメーターで指定されたバイト数を超えないと変換できない場合、このパラメーターは false charCount に設定 byteCount されます。 このような場合、アプリケーションは出力バッファーの内容を使用するか、新しい出力バッファーを提供し、パラメーターで指定された文字数でパラメーターをインクリメントしてから、 メソッドを再度呼び出して残りの入力を処理する必要があります。 chars charsUsed Convert

パラメーター completed と パラメーターが等しい場合でも、 パラメーターを false charsUsedcharCount 設定することもできます。 この状況は、バッファーに格納されていないデータがまだ オブジェクト Encoder に存在する場合に発生 chars します。

適用対象

Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean)

Unicode 文字の配列をエンコード済みバイト シーケンスに変換し、その結果をバイト配列に格納します。

public:
 virtual void Convert(cli::array <char> ^ chars, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, bool flush, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] bool % completed);
public virtual void Convert (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Convert (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed);
abstract member Convert : char[] * int * int * byte[] * int * int * bool * int * int * bool -> unit
override this.Convert : char[] * int * int * byte[] * int * int * bool * int * int * bool -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member Convert : char[] * int * int * byte[] * int * int * bool * int * int * bool -> unit
override this.Convert : char[] * int * int * byte[] * int * int * bool * int * int * bool -> unit
Public Overridable Sub Convert (chars As Char(), charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer, byteCount As Integer, flush As Boolean, ByRef charsUsed As Integer, ByRef bytesUsed As Integer, ByRef completed As Boolean)

パラメーター

chars
Char[]

変換する文字の配列。

charIndex
Int32

変換する chars の最初の要素。

charCount
Int32

変換する chars の要素の数。

bytes
Byte[]

変換後のバイトを格納する配列。

byteIndex
Int32

データを格納する bytes の最初の要素。

byteCount
Int32

変換に使用する bytes 内の最大要素数。

flush
Boolean

これ以上データの変換を行わないことを示す場合は true。それ以外の場合は false

charsUsed
Int32

このメソッドから制御が戻るときに、chars で変換に使用された文字数を格納します。 このパラメーターは初期化せずに渡されます。

bytesUsed
Int32

このメソッドから制御が戻るときに、変換の結果生成されたバイト数を格納します。 このパラメーターは初期化せずに渡されます。

completed
Boolean

このメソッドから制御が戻るときに、charCount で指定したすべての文字が変換された場合は true。それ以外の場合は false。 このパラメーターは初期化せずに渡されます。

属性

例外

chars または bytesnull (Nothing) です。

charIndexcharCountbyteIndex、または byteCount が 0 未満です。

または

chars - charIndex の長さは charCount 未満です。

または

bytes - byteIndex の長さは byteCount 未満です。

出力バッファーが小さすぎて、変換した入力を格納できません。 出力バッファーは、GetByteCount メソッドで示されるサイズ以上である必要があります。

フォールバックが発生しました (詳細については「.NET での文字エンコード」を参照)

および

FallbackEncoderExceptionFallback に設定されます。

次の例では、メソッドを使用して Convert utf-16 文字のファイルを utf-8 に変換し、メソッドを使用して utf-8 文字 Convert を utf-16 文字に戻します。

// This code example demonstrates the Encoder.Convert() and Decoder.Convert methods.
// This example uses files for input and output, but any source that can be expressed
// as a stream can be used instead.

    using System;
    using System.Text;
    using System.IO;

    public class Sample
    {
    static void Main(string[] args)
        {
// Create a large file of UTF-16 encoded Unicode characters. The file is named Example.txt,
// and is used as input to the Encoder.Convert() method.

            CreateTestFile("Example.txt");

// Using an input file of UTF-16 encoded characters named Example.txt, create an output file
// of UTF-8 encoded bytes named UTF8.txt.

            EncoderConvert("Example.txt", "UTF8.txt", Encoding.UTF8);

// Using an input file of UTF-8 encoded bytes named UTF8.txt, create an output file
// of UTF-16 encoded characters named UTF16.txt.

            DecoderConvert("UTF8.txt", "UTF16.txt", Encoding.UTF8);
        }

// --------------------------------------------------------------------------------------------
// Use the Encoder.Convert() method to convert a file of characters to a file of encoded bytes.
// --------------------------------------------------------------------------------------------
        static void EncoderConvert(String inputFileName, String outputFileName, Encoding enc)
        {
// Convert an input file of characters to an output file of encoded bytes.
// StreamWriter could convert the input file for us, but we'll perform the conversion
// ourselves.

            FileStream fs = new FileStream(outputFileName, FileMode.Create);
            BinaryWriter outputFile = new BinaryWriter(fs);

// StreamReader will detect Unicode encoding from the Byte Order Mark that heads the input file.
            StreamReader inputFile = new StreamReader(inputFileName);

// Get an Encoder.
            Encoder encoder = enc.GetEncoder();

// Guarantee the output buffer large enough to convert a few characters.
            int UseBufferSize = 64;
            if (UseBufferSize < enc.GetMaxByteCount(10))
                    UseBufferSize = enc.GetMaxByteCount(10);
            byte[] bytes = new byte[UseBufferSize];

// Intentionally make the input character buffer larger than the output byte buffer so the
// conversion loop executes more than one cycle.

            char[] chars = new char[UseBufferSize * 4];
            int charsRead;
            do
            {
// Read at most the number of characters that will fit in the input buffer. The return
// value is the actual number of characters read, or zero if no characters remain.
                charsRead = inputFile.Read(chars, 0, UseBufferSize * 4);

                bool completed = false;
                int charIndex = 0;
                int charsUsed;
                int bytesUsed;

                while (!completed)
                {
// If this is the last input data, flush the encoder's internal buffer and state.

                    bool flush = (charsRead == 0);
                    encoder.Convert(chars, charIndex, charsRead - charIndex,
                                    bytes, 0, UseBufferSize, flush,
                                    out charsUsed, out bytesUsed, out completed);

// The conversion produced the number of bytes indicated by bytesUsed. Write that number
// of bytes to the output file.
                    outputFile.Write(bytes, 0, bytesUsed);

// Increment charIndex to the next block of characters in the input buffer, if any, to convert.
                    charIndex += charsUsed;
                }
            }
            while(charsRead != 0);

            outputFile.Close();
            fs.Close();
            inputFile.Close();
        }

// --------------------------------------------------------------------------------------------
// Use the Decoder.Convert() method to convert a file of encoded bytes to a file of characters.
// --------------------------------------------------------------------------------------------
        static void DecoderConvert(String inputFileName, String outputFileName, Encoding enc)
        {
// Convert an input file of of encoded bytes to an output file characters.
// StreamWriter could convert the input file for us, but we'll perform the conversion
// ourselves.

            StreamWriter outputFile = new StreamWriter(outputFileName, false, Encoding.Unicode);

// Read the input as a binary file so we can detect the Byte Order Mark.
            FileStream fs = new FileStream(inputFileName, FileMode.Open);
            BinaryReader inputFile = new BinaryReader(fs);

// Get a Decoder.
            Decoder decoder = enc.GetDecoder();

// Guarantee the output buffer large enough to convert a few characters.
            int UseBufferSize = 64;
            if (UseBufferSize < enc.GetMaxCharCount(10))
                    UseBufferSize = enc.GetMaxCharCount(10);
            char[] chars = new char[UseBufferSize];

// Intentionally make the input byte buffer larger than the output character buffer so the
// conversion loop executes more than one cycle.

            byte[] bytes = new byte[UseBufferSize * 4];
            int bytesRead;
            do
            {
// Read at most the number of bytes that will fit in the input buffer. The
// return value is the actual number of bytes read, or zero if no bytes remain.

                bytesRead = inputFile.Read(bytes, 0, UseBufferSize * 4);

                bool completed = false;
                int byteIndex = 0;
                int bytesUsed;
                int charsUsed;

                while (!completed)
                {
// If this is the last input data, flush the decoder's internal buffer and state.

                    bool flush = (bytesRead == 0);
                    decoder.Convert(bytes, byteIndex, bytesRead - byteIndex,
                                    chars, 0, UseBufferSize, flush,
                                    out bytesUsed, out charsUsed, out completed);

// The conversion produced the number of characters indicated by charsUsed. Write that number
// of characters to the output file.

                    outputFile.Write(chars, 0, charsUsed);

// Increment byteIndex to the next block of bytes in the input buffer, if any, to convert.
                    byteIndex += bytesUsed;
                }
            }
            while(bytesRead != 0);

            outputFile.Close();
            fs.Close();
            inputFile.Close();
        }

// --------------------------------------------------------------------------------------------
// Create a large file of UTF-16 encoded Unicode characters.
// --------------------------------------------------------------------------------------------
        static void CreateTestFile(String FileName)
        {
// StreamWriter defaults to UTF-8 encoding so explicitly specify Unicode, that is,
// UTF-16, encoding.
            StreamWriter file = new StreamWriter(FileName, false, Encoding.Unicode);

// Write a line of text 100 times.
            for (int i = 0; i < 100; i++)
            {
                file.WriteLine("This is an example input file used by the convert example.");
            }

// Write Unicode characters from U+0000 to, but not including, the surrogate character range.
            for (char c = (char)0; c < (char)0xD800; c++)
            {
                file.Write(c);
            }
            file.Close();
        }
    }

/*
This code example produces the following results:

(Execute the -dir- console window command and examine the files created.)

Example.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
UTF8.txt, which contains 169,712 UTF-8 encoded bytes.
UTF16.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).

(Execute the -comp- console window command and compare the two Unicode files.)

>comp example.txt utf16.txt /L
Comparing example.txt and utf16.txt...
Files compare OK

(The two files are equal.)

*/
' This code example demonstrates the Encoder.Convert() and Decoder.Convert methods. 
' This example uses files for input and output, but any source that can be expressed
' as a stream can be used instead.

Imports System.Text
Imports System.IO

Public Class Sample
    Shared Sub Main(ByVal args() As String) 
        ' Create a large file of UTF-16 encoded Unicode characters. The file is named Example.txt, 
        ' and is used as input to the Encoder.Convert() method. 
        CreateTestFile("Example.txt")
        
        
        ' Using an input file of UTF-16 encoded characters named Example.txt, create an output file 
        ' of UTF-8 encoded bytes named UTF8.txt.
        EncoderConvert("Example.txt", "UTF8.txt", Encoding.UTF8)
        
        ' Using an input file of UTF-8 encoded bytes named UTF8.txt, create an output file 
        ' of UTF-16 encoded characters named UTF16.txt.
        DecoderConvert("UTF8.txt", "UTF16.txt", Encoding.UTF8)
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Use the Encoder.Convert() method to convert a file of characters to a file of encoded bytes.
    ' --------------------------------------------------------------------------------------------
    Shared Sub EncoderConvert(ByVal inputFileName As String, ByVal outputFileName As String, ByVal enc As Encoding) 
        ' Convert an input file of characters to an output file of encoded bytes.
        ' StreamWriter could convert the input file for us, but we'll perform the conversion 
        ' ourselves.
        Dim fs As New FileStream(outputFileName, FileMode.Create)
        Dim outputFile As New BinaryWriter(fs)
        
        ' StreamReader will detect Unicode encoding from the Byte Order Mark that heads the input file.
        Dim inputFile As New StreamReader(inputFileName)
        
        ' Get an Encoder.
        Dim encoder As Encoder = enc.GetEncoder()
        
        ' Guarantee the output buffer large enough to convert a few characters.
        Dim UseBufferSize As Integer = 64
        If UseBufferSize < enc.GetMaxByteCount(10) Then
            UseBufferSize = enc.GetMaxByteCount(10)
        End If
        Dim bytes(UseBufferSize) As Byte
        
        ' Intentionally make the input character buffer larger than the output byte buffer so the 
        ' conversion loop executes more than one cycle. 
        Dim chars(UseBufferSize * 4) As Char
        Dim charsRead As Integer
        Do
            ' Read at most the number of characters that will fit in the input buffer. The return 
            ' value is the actual number of characters read, or zero if no characters remain. 
            charsRead = inputFile.Read(chars, 0, UseBufferSize * 4)
            
            Dim completed As Boolean = False
            Dim charIndex As Integer = 0
            Dim charsUsed As Integer
            Dim bytesUsed As Integer
            
            While Not completed
                ' If this is the last input data, flush the encoder's internal buffer and state.
                Dim flush As Boolean = charsRead = 0
                encoder.Convert(chars, charIndex, charsRead - charIndex, bytes, 0, UseBufferSize, flush, charsUsed, bytesUsed, completed)
                
                ' The conversion produced the number of bytes indicated by bytesUsed. Write that number
                ' of bytes to the output file.
                outputFile.Write(bytes, 0, bytesUsed)
                
                ' Increment charIndex to the next block of characters in the input buffer, if any, to convert.
                charIndex += charsUsed
            End While
        Loop While charsRead <> 0
        
        outputFile.Close()
        fs.Close()
        inputFile.Close()
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Use the Decoder.Convert() method to convert a file of encoded bytes to a file of characters.
    ' --------------------------------------------------------------------------------------------
    Shared Sub DecoderConvert(ByVal inputFileName As String, ByVal outputFileName As String, ByVal enc As Encoding) 
        ' Convert an input file of of encoded bytes to an output file characters.
        ' StreamWriter could convert the input file for us, but we'll perform the conversion 
        ' ourselves.
        Dim outputFile As New StreamWriter(outputFileName, False, Encoding.Unicode)
        
        ' Read the input as a binary file so we can detect the Byte Order Mark.
        Dim fs As New FileStream(inputFileName, FileMode.Open)
        Dim inputFile As New BinaryReader(fs)
        
        ' Get a Decoder.
        Dim decoder As Decoder = enc.GetDecoder()
        
        ' Guarantee the output buffer large enough to convert a few characters.
        Dim UseBufferSize As Integer = 64
        If UseBufferSize < enc.GetMaxCharCount(10) Then
            UseBufferSize = enc.GetMaxCharCount(10)
        End If
        Dim chars(UseBufferSize) As Char
        
        ' Intentionally make the input byte buffer larger than the output character buffer so the 
        ' conversion loop executes more than one cycle. 
        Dim bytes(UseBufferSize * 4) As Byte
        Dim bytesRead As Integer
        Do
            ' Read at most the number of bytes that will fit in the input buffer. The 
            ' return value is the actual number of bytes read, or zero if no bytes remain. 
            bytesRead = inputFile.Read(bytes, 0, UseBufferSize * 4)
            
            Dim completed As Boolean = False
            Dim byteIndex As Integer = 0
            Dim bytesUsed As Integer
            Dim charsUsed As Integer
            
            While Not completed
                ' If this is the last input data, flush the decoder's internal buffer and state.
                Dim flush As Boolean = bytesRead = 0
                decoder.Convert(bytes, byteIndex, bytesRead - byteIndex, chars, 0, UseBufferSize, flush, bytesUsed, charsUsed, completed)
                
                ' The conversion produced the number of characters indicated by charsUsed. Write that number
                ' of characters to the output file.
                outputFile.Write(chars, 0, charsUsed)
                
                ' Increment byteIndex to the next block of bytes in the input buffer, if any, to convert.
                byteIndex += bytesUsed
            End While
        Loop While bytesRead <> 0
        
        outputFile.Close()
        fs.Close()
        inputFile.Close()
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Create a large file of UTF-16 encoded Unicode characters. 
    ' --------------------------------------------------------------------------------------------
    Shared Sub CreateTestFile(ByVal FileName As String) 
        ' StreamWriter defaults to UTF-8 encoding so explicitly specify Unicode, that is, 
        ' UTF-16, encoding.
        Dim file As New StreamWriter(FileName, False, Encoding.Unicode)
        
        ' Write a line of text 100 times.
        Dim i As Integer
        For i = 0 To 99
            file.WriteLine("This is an example input file used by the convert example.")
        Next i
        
        ' Write Unicode characters from U+0000 to, but not including, the surrogate character range.
        Dim c As Integer
        For c = 0 To &HD800
            file.Write(ChrW(c))
        Next c
        file.Close()
    
    End Sub
End Class

'
'This code example produces the following results:
'
'(Execute the -dir- console window command and examine the files created.)
'
'Example.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
'UTF8.txt, which contains 169,712 UTF-8 encoded bytes.
'UTF16.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
'
'(Execute the -comp- console window command and compare the two Unicode files.)
'
'>comp example.txt utf16.txt /L
'Comparing example.txt and utf16.txt...
'Files compare OK
'
'(The two files are equal.)
'

注釈

オブジェクトは、 の Encoder 呼び出し間に状態を保存します Convert 。 アプリケーションがデータストリームを使用して完了したら、 パラメーターを に設定して、状態情報がフラッシュ flush true される必要があります。 この設定では、エンコーダーはデータ ブロックの末尾にある無効なバイトを無視し、内部バッファーをクリアします。 サロゲート ペアの高サロゲートなど、論理ユニットの一部である残りの処理されたデータは、現在のフォールバック設定に従って変換されます。

メソッドは、ファイルやストリームから読み取ったデータなど、任意の量の入力をエンコードするためにループで使用 Convert するように設計されています。 これは、エンコード操作の出力を固定サイズのバッファーに格納します。 GetBytes は、出力バッファーのサイズが十分ではない場合は例外をスローしますが、可能な限り多くの領域を埋め、読み取った文字と書き込まれたバイト Convert を返します。 その他のコメント Encoding.GetBytes については、記事も参照してください。

completed出力パラメーターは、入力バッファー内のすべてのデータが変換され、出力バッファーに格納されたかどうかを示します。 パラメーターで指定した文字数が、 false charCount パラメーターで指定されたバイト数を超えることなく変換できない場合、このパラメーターはに設定され byteCount ます。 そのような状況では、アプリケーションは出力バッファーの内容を使用するか、新しい出力バッファーを提供し、パラメーター chars によって指定された文字数だけパラメーターをインクリメント charsUsed してから、メソッドを再度呼び出して Convert 残りの入力を処理する必要があります。

completedパラメーター false charsUsed とパラメーターが等しい場合でも、パラメーターをに設定することもでき charCount ます。 この状況は、 Encoder バッファーに格納されていないデータがオブジェクトにまだ存在する場合に発生し chars ます。

適用対象