UTF8Encoding クラス
アセンブリ: mscorlib (mscorlib.dll 内)


エンコーディングは、Unicode 文字のセットをバイト シーケンスに変換するプロセスです。デコードはその逆になります。エンコードされたバイト シーケンスを Unicode 文字のセットに変換するプロセスです。
Unicode Standard では、サポートされるすべてのスクリプトについて、各文字にコード ポイント (数値) を割り当てています。コード ポイントのエンコードには UTF (Unicode Transformation Format) が使用されます。Unicode Standard バージョン 3.2 では、次の UTF が使用されています。
GetByteCount メソッドは、Unicode 文字のセットをエンコードした結果得られるバイト数を確認します。実際のエンコードは、GetBytes メソッドによって実行されます。
同様に、GetCharCount メソッドは、バイト シーケンスをデコードした結果得られる文字数を確認します。実際のデコードは、GetChars メソッドと GetString メソッドによって実行されます。
UTF8Encoding は、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、EF BB BF (16 進数) としてシリアル化されます。GetPreamble メソッドは、バイト順マークを格納するバイト配列を返します。
Unicode エンコーディング、バイト順、およびバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。
![]() |
---|
エラー検出を有効にしてクラス インスタンスの安全性を向上させるには、throwOnInvalidBytes パラメータを受け取る UTF8Encoding コンストラクタを使用し、このパラメータを true に設定します。エラー検出を有効にすると、メソッドは、無効な文字シーケンスやバイト シーケンスを検出したときに、ArgumentException をスローします。エラー検出を無効にすると、例外はスローされず、無効なシーケンスは全般に無視されます。 |
UTF8Encoding は、Windows コード ページ 65001 に対応しています。
![]() |
---|
オブジェクトのシリアル化と逆シリアル化を行う際に使用した .NET Framework のバージョンが異なる場合、UTF-8 でエンコードされたオブジェクトの状態は保持されません。 |

次の例は、UTF8Encoding を使用して、Unicode 文字列をエンコードし、その結果をバイト配列に格納する方法を示しています。encodedBytes を文字列にデコードしても、データの損失は発生しません。
Imports System Imports System.Text Imports Microsoft.VisualBasic.Strings Class UTF8EncodingExample Public Shared Sub Main() ' Create a UTF-8 encoding. Dim utf8 As New UTF8Encoding() ' A Unicode string with two characters outside an 8-bit code range. Dim unicodeString As String = _ "This unicode string contains two characters " & _ "with codes outside an 8-bit code range, " & _ "Pi (" & ChrW(928) & ") and Sigma (" & ChrW(931) & ")." Console.WriteLine("Original string:") Console.WriteLine(unicodeString) ' Encode the string. Dim encodedBytes As Byte() = utf8.GetBytes(unicodeString) Console.WriteLine() Console.WriteLine("Encoded bytes:") Dim b As Byte For Each b In encodedBytes Console.Write("[{0}]", b) Next b Console.WriteLine() ' Decode bytes back to string. ' Notice Pi and Sigma characters are still present. Dim decodedString As String = utf8.GetString(encodedBytes) Console.WriteLine() Console.WriteLine("Decoded bytes:") Console.WriteLine(decodedString) End Sub End Class
using System; using System.Text; class UTF8EncodingExample { public static void Main() { // Create a UTF-8 encoding. UTF8Encoding utf8 = new UTF8Encoding(); // A Unicode string with two characters outside an 8-bit code range. String unicodeString = "This unicode string contains two characters " + "with codes outside an 8-bit code range, " + "Pi (\u03a0) and Sigma (\u03a3)."; Console.WriteLine("Original string:"); Console.WriteLine(unicodeString); // Encode the string. Byte[] encodedBytes = utf8.GetBytes(unicodeString); Console.WriteLine(); Console.WriteLine("Encoded bytes:"); foreach (Byte b in encodedBytes) { Console.Write("[{0}]", b); } Console.WriteLine(); // Decode bytes back to string. // Notice Pi and Sigma characters are still present. String decodedString = utf8.GetString(encodedBytes); Console.WriteLine(); Console.WriteLine("Decoded bytes:"); Console.WriteLine(decodedString); } }
using namespace System; using namespace System::Text; using namespace System::Collections; int main() { // Create a UTF-8 encoding. UTF8Encoding^ utf8 = gcnew UTF8Encoding; // A Unicode string with two characters outside an 8-bit code range. String^ unicodeString = L"This unicode string contains two characters with codes outside an 8-bit code range, Pi (\u03a0) and Sigma (\u03a3)."; Console::WriteLine( "Original string:" ); Console::WriteLine( unicodeString ); // Encode the string. array<Byte>^encodedBytes = utf8->GetBytes( unicodeString ); Console::WriteLine(); Console::WriteLine( "Encoded bytes:" ); IEnumerator^ myEnum = encodedBytes->GetEnumerator(); while ( myEnum->MoveNext() ) { Byte b = safe_cast<Byte>(myEnum->Current); Console::Write( "[{0}]", b ); } Console::WriteLine(); // Decode bytes back to string. // Notice Pi and Sigma characters are still present. String^ decodedString = utf8->GetString( encodedBytes ); Console::WriteLine(); Console::WriteLine( "Decoded bytes:" ); Console::WriteLine( decodedString ); }
import System.*; import System.Text.*; class UTF8EncodingExample { public static void main(String[] args) { // Create a UTF-8 encoding. UTF8Encoding utf8 = new UTF8Encoding(); // A Unicode string with two characters outside an 8-bit code range. String unicodeString = "This unicode string contains two characters " + "with codes outside an 8-bit code range, " + "Pi (\u03a0) and Sigma (\u03a3)."; Console.WriteLine("Original string:"); Console.WriteLine(unicodeString); // Encode the string. ubyte encodedBytes[] = utf8.GetBytes(unicodeString); Console.WriteLine(); Console.WriteLine("Encoded bytes:"); for (int iCtr = 0; iCtr < encodedBytes.length; iCtr++) { ubyte b = encodedBytes[iCtr]; Console.Write("[{0}]", String.valueOf(b)); } Console.WriteLine(); // Decode bytes back to string. // Notice Pi and Sigma characters are still present. String decodedString = utf8.GetString(encodedBytes); Console.WriteLine(); Console.WriteLine("Decoded bytes:"); Console.WriteLine(decodedString); } //main } //UTF8EncodingExample

System.Text.Encoding
System.Text.UTF8Encoding


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 コンストラクタ ()
アセンブリ: mscorlib (mscorlib.dll 内)


このコンストラクタは、Unicode バイト順マークを付加せず、無効なエンコーディングが検出されたときに例外をスローしないインスタンスを作成します。
![]() |
---|
セキュリティ上の理由から、throwOnInvalidBytes パラメータを受け取るコンストラクタを使用して、このパラメータを true に設定することにより、エラー検出を有効にすることをお勧めします。 |
UTF8Encoding は、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、EF BB BF (16 進数) としてシリアル化されます。GetPreamble メソッドは、バイト順マークを格納するバイト配列を返します。
Unicode エンコーディング、バイト順、およびバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。

次のコード例は、新しい UTF8Encoding インスタンスを作成してエンコード名を表示する方法を示しています。
Imports System Imports System.Text Class UTF8EncodingExample Public Shared Sub Main() Dim utf8 As New UTF8Encoding() Dim encodingName As String = utf8.EncodingName Console.WriteLine("Encoding name: " & encodingName) End Sub 'Main End Class 'UTF8EncodingExample
using System; using System.Text; class UTF8EncodingExample { public static void Main() { UTF8Encoding utf8 = new UTF8Encoding(); String encodingName = utf8.EncodingName; Console.WriteLine("Encoding name: " + encodingName); } }

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 コンストラクタ (Boolean, Boolean)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim encoderShouldEmitUTF8Identifier As Boolean Dim throwOnInvalidBytes As Boolean Dim instance As New UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes)
public function UTF8Encoding ( encoderShouldEmitUTF8Identifier : boolean, throwOnInvalidBytes : boolean )

throwOnInvalidBytes が true の場合、無効なバイト シーケンスを検出したメソッドは System.ArgumentException をスローします。それ以外の場合は、例外をスローせず、無効なシーケンスを無視します。
![]() |
---|
セキュリティ上の理由から、このコンストラクタを使用して UTF8Encoding クラスのインスタンスを作成し、throwOnInvalidBytes を true に設定することでエラー検出をオンにすることをお勧めします。 |
UTF8Encoding は、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、EF BB BF (16 進数) としてシリアル化されます。GetPreamble メソッドは、バイト順マークを格納するバイト配列を返します。
Unicode エンコーディング、バイト順、およびバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。

次の例は、エンコーディング時に Unicode バイト順マーク プリフィックスを作成せず、無効なエンコーディングが検出された場合には例外をスローするよう指定して、新しい UTF8Encoding インスタンスを作成する方法を示しています。このコンストラクタの動作を、無効なエンコーディングが検出された場合でも例外をスローしない UTF8Encoding の既定のコンストラクタと比較してください。
Imports System Imports System.Text Imports Microsoft.VisualBasic 'Imports Microsoft.VisualBasic.Strings Class UTF8EncodingExample Public Shared Sub Main() Dim utf8 As New UTF8Encoding() Dim utf8ThrowException As New UTF8Encoding(False, True) ' This array contains two high surrogates in a row: ' ChrW(55297) and ChrW(55298). ' A high surrogate should be followed by a low surrogate. Dim chars() As Char = {"a"c, "b"c, "c"c, ChrW(55297), ChrW(55298), "d"c} ' The following method call will not throw an exception. Dim bytes As Byte() = utf8.GetBytes(chars) ShowArray(bytes) Try ' The following method call will throw an exception. bytes = utf8ThrowException.GetBytes(chars) Catch e As Exception Console.WriteLine("Exception raised. " + ControlChars.Cr + "Message: {0}", e.Message) End Try End Sub Public Shared Sub ShowArray(theArray As Array) Dim o As Object For Each o In theArray Console.Write("[{0}]", o) Next o Console.WriteLine() End Sub End Class
using System; using System.Text; class UTF8EncodingExample { public static void Main() { UTF8Encoding utf8 = new UTF8Encoding(); UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true); // This array contains two high surrogates in a row (\uD801, \uD802). // A high surrogate should be followed by a low surrogate. Char[] chars = new Char[] {'a', 'b', 'c', '\uD801', '\uD802', 'd'}; // The following method call will not throw an exception. Byte[] bytes = utf8.GetBytes(chars); ShowArray(bytes); try { // The following method call will throw an exception. bytes = utf8ThrowException.GetBytes(chars); } catch (Exception e) { Console.WriteLine("Exception raised. \nMessage: {0}", e.Message); } } public static void ShowArray(Array theArray) { foreach (Object o in theArray) { Console.Write("[{0}]", o); } Console.WriteLine(); } }
using namespace System; using namespace System::Text; using namespace System::Collections; void ShowArray( Array^ theArray ) { IEnumerator^ myEnum = theArray->GetEnumerator(); while ( myEnum->MoveNext() ) { Object^ o = safe_cast<Object^>(myEnum->Current); Console::Write( "[{0}]", o ); } Console::WriteLine(); } int main() { UTF8Encoding^ utf8 = gcnew UTF8Encoding; UTF8Encoding^ utf8ThrowException = gcnew UTF8Encoding( false,true ); // This array contains two high surrogates in a row (\uD801, \uD802). // A high surrogate should be followed by a low surrogate. array<Char>^chars = {'a','b','c',L'\xD801',L'\xD802','d'}; // The following method call will not throw an exception. array<Byte>^bytes = utf8->GetBytes( chars ); ShowArray( bytes ); try { // The following method call will throw an exception. bytes = utf8ThrowException->GetBytes( chars ); } catch ( Exception^ e ) { Console::WriteLine( "Exception raised. \nMessage: {0}", e->Message ); } }
import System.*; import System.Text.*; class UTF8EncodingExample { public static void main(String[] args) { UTF8Encoding utf8 = new UTF8Encoding(); UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true); // This array contains two high surrogates in a row (\uD801, \uD802). // A high surrogate should be followed by a low surrogate. char chars[] = new char[] { 'a', 'b', 'c', '\uD801', '\uD802', 'd' }; // The following method call will not throw an exception. ubyte bytes[] = utf8.GetBytes(chars); ShowArray(bytes); try { // The following method call will throw an exception. bytes = utf8ThrowException.GetBytes(chars); } catch(System.Exception e) { Console.WriteLine("Exception raised. \nMessage: {0}", e.get_Message()); } } //main public static void ShowArray(Array theArray) { Object o = null; for (int iCtr=0; iCtr < theArray.get_Count(); iCtr++) { o = theArray.get_Item(iCtr); Console.Write("[{0}]", o); } Console.WriteLine(); } //ShowArray } //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 コンストラクタ (Boolean)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim encoderShouldEmitUTF8Identifier As Boolean Dim instance As New UTF8Encoding(encoderShouldEmitUTF8Identifier)

このコンストラクタは、無効なエンコーディングを検出したときに例外をスローしないインスタンスを作成します。
![]() |
---|
セキュリティ上の理由から、throwOnInvalidBytes パラメータを受け取るコンストラクタを使用して、このパラメータを true に設定することにより、エラー検出を有効にすることをお勧めします。 |
UTF8Encoding は、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、EF BB BF (16 進数) としてシリアル化されます。GetPreamble メソッドは、バイト順マークを格納するバイト配列を返します。
Unicode エンコーディング、バイト順、およびバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。

次の例は、エンコーディング時に Unicode バイト順マーク プリフィックスを作成するよう指定して、新しい UTF8Encoding インスタンスを作成する方法を示しています。GetPreamble メソッドは、Unicode バイト順マーク プリフィックスを返し、コンソールに表示します。既定のコンストラクタを使用して作成した UTF8Encoding には、Unicode バイト順マーク プリフィックスはありません。
Imports System Imports System.Text Class UTF8EncodingExample Public Shared Sub Main() Dim utf8 As New UTF8Encoding() Dim utf8EmitBOM As New UTF8Encoding(True) Console.WriteLine("utf8 preamble:") ShowArray(utf8.GetPreamble()) Console.WriteLine("utf8EmitBOM:") ShowArray(utf8EmitBOM.GetPreamble()) End Sub 'Main Public Shared Sub ShowArray(theArray As Array) Dim o As Object For Each o In theArray Console.Write("[{0}]", o) Next o Console.WriteLine() End Sub 'ShowArray End Class 'UTF8EncodingExample
using System; using System.Text; class UTF8EncodingExample { public static void Main() { UTF8Encoding utf8 = new UTF8Encoding(); UTF8Encoding utf8EmitBOM = new UTF8Encoding(true); Console.WriteLine("utf8 preamble:"); ShowArray(utf8.GetPreamble()); Console.WriteLine("utf8EmitBOM:"); ShowArray(utf8EmitBOM.GetPreamble()); } public static void ShowArray(Array theArray) { foreach (Object o in theArray) { Console.Write("[{0}]", o); } Console.WriteLine(); } }
using namespace System; using namespace System::Text; using namespace System::Collections; void ShowArray( Array^ theArray ) { IEnumerator^ myEnum = theArray->GetEnumerator(); while ( myEnum->MoveNext() ) { Object^ o = safe_cast<Object^>(myEnum->Current); Console::Write( "[{0}]", o ); } Console::WriteLine(); } int main() { UTF8Encoding^ utf8 = gcnew UTF8Encoding; UTF8Encoding^ utf8EmitBOM = gcnew UTF8Encoding( true ); Console::WriteLine( "utf8 preamble:" ); ShowArray( utf8->GetPreamble() ); Console::WriteLine( "utf8EmitBOM:" ); ShowArray( utf8EmitBOM->GetPreamble() ); }
import System.*; import System.Text.*; class UTF8EncodingExample { public static void main(String[] args) { UTF8Encoding utf8 = new UTF8Encoding(); UTF8Encoding utf8EmitBOM = new UTF8Encoding(true); Console.WriteLine("utf8 preamble:"); ShowArray(utf8.GetPreamble()); Console.WriteLine("utf8EmitBOM:"); ShowArray(utf8EmitBOM.GetPreamble()); } //main public static void ShowArray(Array theArray) { Object o = null; for (int iCtr = 0; iCtr < theArray.get_Length(); iCtr++) { o = theArray.get_Item(iCtr); Console.Write("[{0}]", o); } Console.WriteLine(); } //ShowArray } //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 コンストラクタ

名前 | 説明 |
---|---|
UTF8Encoding () | UTF8Encoding クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
UTF8Encoding (Boolean) | UTF8Encoding クラスの新しいインスタンスを初期化します。パラメータでは、Unicode バイト順マークを付加するかどうかを指定します。 .NET Compact Framework によってサポートされています。 |
UTF8Encoding (Boolean, Boolean) | UTF8Encoding クラスの新しいインスタンスを初期化します。パラメータでは、Unicode バイト順マークを付加するかどうか、および無効なエンコーディングが検出されたときに例外をスローするかどうかを指定します。 .NET Compact Framework によってサポートされています。 |

UTF8Encoding プロパティ


UTF8Encoding メソッド


名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

UTF8Encoding メンバ
Unicode 文字の UTF-8 エンコーディングを表します。
UTF8Encoding データ型で公開されるメンバを以下の表に示します。




名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

Weblioに収録されているすべての辞書からUTF8Encodingを検索する場合は、下記のリンクをクリックしてください。

- UTF8Encodingのページへのリンク