共用方式為


Decoder.Convert 方法

定義

將編碼的位元組序列轉換成字串或字元陣列。

多載

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

將編碼位元組的範圍轉換為 UTF-16 編碼字元,並將結果儲存在另一個範圍緩衝區中。

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

將已編碼位元組的緩衝區轉換成 UTF-16 編碼的字元,並將結果儲存在另一個緩衝區。

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

將已編碼位元組的陣列轉換成 UTF-16 編碼的字元,並將結果儲存在另一個字元陣列中。

備註

請記住,物件會在 Decoder 呼叫 Convert之間儲存狀態。 當應用程式使用數據流完成時,它應該將 參數設定 flushtrue ,以確保已排清狀態資訊。 使用此設定時,譯碼器會忽略數據區塊結尾的無效位元組,並清除內部緩衝區。 屬於邏輯單元一部分的任何剩餘處理數據,例如 Surrogate 配對的高 Surrogate,會根據目前的後援設定來轉換。

方法 Convert 的設計目的是要用於迴圈,以譯碼任意數量的輸入,例如從檔案或數據流讀取的數據。 它會將譯碼作業的輸出儲存在固定大小緩衝區中。 GetChars 如果輸出緩衝區不夠大,但會盡可能填滿空間,並 Convert 傳回寫入的位元組和字元,前提是輸出數位允許至少兩個字元,就會擲回例外狀況。 另請參閱 Encoding.GetChars 以取得更多批注。

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

來源:
Decoder.cs
來源:
Decoder.cs
來源:
Decoder.cs

將編碼位元組的範圍轉換為 UTF-16 編碼字元,並將結果儲存在另一個範圍緩衝區中。

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

參數

bytes
ReadOnlySpan<Byte>

唯讀位元組範圍,其包含要轉換的序列。

chars
Span<Char>

要儲存轉換字元的範圍。

flush
Boolean

若表示不進行任何其他資料轉換即為 true;否則為 false

bytesUsed
Int32

傳回此方法時,包含轉換所產生的位元組數目。 這個參數會以未初始化的狀態傳遞。

charsUsed
Int32

傳回此方法時,包含來自 chars 為轉換所使用的位元組數目。 這個參數會以未初始化的狀態傳遞。

completed
Boolean

當這個方法傳回時,若所有指定字元皆已轉換,則其中會包含 true;否則為 false。 這個參數會以未初始化的狀態傳遞。

備註

completed輸出參數會指出輸入位元組範圍中的所有資料是否已轉換並儲存在字元範圍中。 如果輸入位元組範圍中包含的位元組數目無法轉換,但不能超過位元範圍中的字元數,這個參數就會設定 false 為 。 在此情況下,應用程式應該使用輸出緩衝區的內容,或提供新的輸出緩衝區、以參數指定的bytesUsed位元組數目遞增bytes參數,然後再次呼叫 Convert 方法來處理剩餘的輸入。

completed即使bytesUsed參數和位元組範圍長度相等,也可以將 參數設定為 false。 如果物件中仍有尚未儲存在bytes範圍中的數據Decoder,就會發生這種情況。

適用於

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

來源:
Decoder.cs
來源:
Decoder.cs
來源:
Decoder.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Text.Decoder.Convert(Byte[], Int32, Int32, Char[], Int32, Int32, Boolean, Int32, Int32, Boolean)

將已編碼位元組的緩衝區轉換成 UTF-16 編碼的字元,並將結果儲存在另一個緩衝區。

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

參數

bytes
Byte*

緩衝區位址,包含要轉換的位元組序列。

byteCount
Int32

bytes 中要轉換的位元組數。

chars
Char*

緩衝區位址,用來儲存已轉換的字元。

charCount
Int32

chars 中要用於轉換的字元數上限。

flush
Boolean

若表示不進行任何其他資料轉換即為 true;否則為 false

bytesUsed
Int32

傳回此方法時,包含轉換所產生的位元組數目。 這個參數會以未初始化的狀態傳遞。

charsUsed
Int32

傳回此方法時,包含來自 chars 為轉換所使用的位元組數目。 這個參數會以未初始化的狀態傳遞。

completed
Boolean

傳回此方法時,若所有 byteCount 指定的字元皆已轉換則包含 true;否則為 false。 這個參數會以未初始化的狀態傳遞。

屬性

例外狀況

charsbytesnull (Nothing)。

charCountbyteCount 小於零。

輸出緩衝區太小而無法包含任何已轉換的輸入。 輸出緩衝區的大小至少應為 2 個字元,以便包含至少一對代理字元組。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

Fallback 設定為 DecoderExceptionFallback

備註

completed輸出參數會指出輸入緩衝區中的所有數據是否已轉換並儲存在輸出緩衝區中。 如果參數指定的byteCount位元組數目無法轉換,但未超過 參數所指定的字元數,這個參數就會設定falsecharCount 。 在此情況下,應用程式應該使用輸出緩衝區的內容,或提供新的輸出緩衝區、以參數指定的bytesUsed位元組數目遞增bytes參數,然後再次呼叫 Convert 方法來處理剩餘的輸入。

completed即使 bytesUsedbyteCount 參數相等,參數也可以設定為 false。 如果物件中仍有尚未儲存在bytes緩衝區中的數據Decoder,就會發生這種情況。

適用於

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

來源:
Decoder.cs
來源:
Decoder.cs
來源:
Decoder.cs

將已編碼位元組的陣列轉換成 UTF-16 編碼的字元,並將結果儲存在另一個字元陣列中。

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

參數

bytes
Byte[]

要轉換的位元組陣列。

byteIndex
Int32

bytes 中要轉換的第一個項目。

byteCount
Int32

要轉換的 bytes 項目數目。

chars
Char[]

要儲存已轉換字元的陣列。

charIndex
Int32

chars 中的第一個項目,其為儲存資料所在。

charCount
Int32

轉換中要使用的 chars 之項目數上限。

flush
Boolean

true 表示不轉換任何其他資料,否則為 false

bytesUsed
Int32

傳回此方法時,包含轉換所使用的位元組數目。 這個參數會以未初始化的狀態傳遞。

charsUsed
Int32

傳回此方法時,包含轉換所產生之來自 chars 的字元數目。 這個參數會以未初始化的狀態傳遞。

completed
Boolean

傳回此方法時,若所有 byteCount 指定的字元皆已轉換則包含 true;否則為 false。 這個參數會以未初始化的狀態傳遞。

屬性

例外狀況

charsbytesnull (Nothing)。

charIndexcharCountbyteIndexbyteCount 小於零。

-或-

chars - charIndex 的長度小於 charCount

-或-

bytes - byteIndex 的長度小於 byteCount

輸出緩衝區太小而無法包含任何已轉換的輸入。 輸出緩衝區的大小至少應為 2 個字元,以便包含至少一對代理字元組。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

Fallback 設定為 DecoderExceptionFallback

範例

下列範例會使用 方法, Convert 將UTF-16字元的檔案轉換成UTF-8。 然後, Convert 它會使用 方法來將UTF-8字元轉換成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.)
'

備註

completed輸出參數會指出輸入緩衝區中的所有數據是否已轉換並儲存在輸出緩衝區中。 如果參數指定的byteCount位元組數目無法轉換,但未超過 參數所指定的字元數,這個參數就會設定falsecharCount 。 在此情況下,應用程式應該使用輸出緩衝區的內容,或提供新的輸出緩衝區、以參數指定的bytesUsed位元組數目遞增bytes參數,然後再次呼叫 Convert 方法來處理剩餘的輸入。

completed即使 bytesUsedbyteCount 參數相等,參數也可以設定為 false。 如果物件中仍有尚未儲存在bytes緩衝區中的數據Decoder,就會發生這種情況。

適用於