UTF8Encoding.GetDecoder メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As UTF8Encoding Dim returnValue As Decoder returnValue = instance.GetDecoder
UTF-8 でエンコードされたバイト シーケンスを Unicode 文字のシーケンスに変換する Decoder。

Decoder.GetChars メソッドは、このクラスの GetChars メソッドと同様の方法で、連続するバイト ブロックを連続する文字ブロックに変換します。ただし、Decoder では、ブロックにまたがるバイト シーケンスを正確にデコードできるように、呼び出し間のステータス情報を維持します。また、Decoder は、データ ブロックの末尾で後続バイトを保持し、その後続バイトを次のデコード操作に使用します。したがって、GetDecoder と GetEncoder は、ネットワーク伝送やファイル操作に役立ちます。これは、ネットワーク伝送やファイル操作では、完全なデータ ストリームではなくデータのブロックを処理することが多いためです。
このインスタンスでエラー検出が有効になっている場合、つまり、コンストラクタの throwOnInvalidCharacters パラメータが true に設定された場合は、このメソッドによって返される Decoder 内でもエラー検出が有効です。エラー検出を有効にしている場合に無効なシーケンスが検出されると、デコーダの状態が定義されていないため、処理は中止されます。

GetDecoder メソッドを使用して UTF-8 デコーダを取得する方法を次の例に示します。デコーダは、バイト シーケンスを文字シーケンスに変換します。
Imports System Imports System.Text Class UTF8EncodingExample Public Shared Sub Main() Dim chars() As Char Dim bytes() As Byte = {99, 204, 128, 234, 130, 160} Dim utf8Decoder As Decoder = Encoding.UTF8.GetDecoder() Dim charCount As Integer = utf8Decoder.GetCharCount(bytes, 0, bytes.Length) chars = New Char(charCount - 1) {} Dim charsDecodedCount As Integer = utf8Decoder.GetChars( _ bytes, 0, bytes.Length, chars, 0 _ ) Console.WriteLine("{0} characters used to decode bytes.", charsDecodedCount) Console.Write("Decoded chars: ") Dim c As Char For Each c In chars Console.Write("[{0}]", c) Next c Console.WriteLine() End Sub 'Main End Class 'UTF8EncodingExample
using System; using System.Text; class UTF8EncodingExample { public static void Main() { Char[] chars; Byte[] bytes = new Byte[] { 99, 204, 128, 234, 130, 160 }; Decoder utf8Decoder = Encoding.UTF8.GetDecoder(); int charCount = utf8Decoder.GetCharCount(bytes, 0, bytes.Length); chars = new Char[charCount]; int charsDecodedCount = utf8Decoder.GetChars(bytes, 0, bytes.Length, chars, 0); Console.WriteLine( "{0} characters used to decode bytes.", charsDecodedCount ); Console.Write("Decoded chars: "); foreach (Char c in chars) { Console.Write("[{0}]", c); } Console.WriteLine(); } }
using namespace System; using namespace System::Text; using namespace System::Collections; int main() { array<Char>^chars; array<Byte>^bytes = {99,204,128,234,130,160}; Decoder^ utf8Decoder = Encoding::UTF8->GetDecoder(); int charCount = utf8Decoder->GetCharCount( bytes, 0, bytes->Length ); chars = gcnew array<Char>(charCount); int charsDecodedCount = utf8Decoder->GetChars( bytes, 0, bytes->Length, chars, 0 ); Console::WriteLine( "{0} characters used to decode bytes.", charsDecodedCount ); Console::Write( "Decoded chars: " ); IEnumerator^ myEnum = chars->GetEnumerator(); while ( myEnum->MoveNext() ) { Char c = safe_cast<Char>(myEnum->Current); Console::Write( "[{0}]", c.ToString() ); } Console::WriteLine(); }
import System .* ; import System.Text .* ; class UTF8EncodingExample { public static void main(String[] args) { char chars[]; ubyte bytes[] = new ubyte[] { 99, 204, 128, 234, 130, 160 }; Decoder utf8Decoder = Encoding.get_UTF8().GetDecoder(); int charCount = utf8Decoder.GetCharCount(bytes, 0, bytes.length); chars = new char[charCount]; int charsDecodedCount = utf8Decoder.GetChars(bytes, 0, bytes.length, chars, 0); Console.WriteLine("{0} characters used to decode bytes.", String.valueOf(charsDecodedCount)); Console.Write("Decoded chars: "); for (int iCtr = 0; iCtr < chars.length; iCtr++) { char c = chars[iCtr]; Console.Write("[{0}]", String.valueOf(c)); } Console.WriteLine(); } //main } //UTF8EncodingExample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- UTF8Encoding.GetDecoder メソッドのページへのリンク