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 コンストラクタ ()のページへのリンク