UnicodeEncodingとは? わかりやすく解説

UnicodeEncoding クラス

Unicode 文字の UTF-16 エンコーディングを表します。

名前空間: System.Text
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class UnicodeEncoding
    Inherits Encoding
Dim instance As UnicodeEncoding
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class UnicodeEncoding : Encoding
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class UnicodeEncoding : public
 Encoding
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class UnicodeEncoding extends Encoding
SerializableAttribute 
ComVisibleAttribute(true) 
public class UnicodeEncoding extends
 Encoding
解説解説

エンコーディングは、Unicode 文字のセットをバイト シーケンスに変換するプロセスです。デコードはその逆になります。エンコードされたバイト シーケンスを Unicode 文字のセットに変換するプロセスです。

Unicode Standard では、サポートされるすべてのスクリプトについて、各文字にコード ポイント (数値) を割り当てています。コード ポイントのエンコードには UTF (Unicode Transformation Format) が使用されます。Unicode Standard バージョン 3.2 では、次の UTF が使用されています。

GetByteCount メソッドは、Unicode 文字のセットをエンコードした結果得られるバイト数を確認します。実際のエンコードは、GetBytes メソッドによって実行されます。

同様に、GetCharCount メソッドは、バイト シーケンスをデコードした結果得られる文字数を確認します。実際のデコードは、GetChars メソッドと GetString メソッドによって実行されます。

エンコーダでは、最上位バイトが先頭に配置されるビッグ エンディアン バイト順、または最下位バイトが先頭に配置されるリトル エンディアン バイト順が使用されます。たとえば、Latin の大文字 A (コード ポイント U+0041) は次のように 16 進数でシリアル化されます。

UnicodeEncoding は、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、次のように 16 進数でシリアル化されます。

通常、ネイティブなバイト順で Unicode 文字を格納した方が効率的です。たとえば、Intel のコンピュータなど、リトル エンディアンのプラットフォームでは、リトル エンディアンのバイト順を使用した方が効率的です。

GetPreamble メソッドは、バイト順マークを格納するバイト配列を返します。このバイト配列がエンコード済みストリームの前に付加されていると、デコーダによる使用エンコーディング形式の識別に役立ちます。

Unicode エンコーディング、バイト順、およびバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。

メモメモ

エラー検出を有効にしてクラス インスタンスの安全性を向上させるには、throwOnInvalidBytes パラメータを受け取る UnicodeEncoding コンストラクタを使用し、このパラメータを true に設定します。エラー検出を有効にすると、メソッドは、無効な文字シーケンスやバイト シーケンスを検出したときに、ArgumentException をスローします。エラー検出を無効にすると、例外はスローされず、無効なシーケンスは全般に無視されます。

UnicodeEncoding は、Windows コード ページ 1200 (リトル エンディアン バイト順) と 1201 (ビッグ エンディアン バイト順) に対応しています。

使用例使用例

次のコード例は、UnicodeEncoding を使用して、Unicode 文字列をバイト配列にエンコードする方法を示しています。データが損失しないことを示すため、バイト配列は文字列にデコードされます。

Imports System
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        ' The encoding.
        Dim uni As New UnicodeEncoding()
        
        ' Create a string that contains Unicode characters.
        Dim unicodeString As String
 = _
            "This Unicode string contains two characters "
 & _
            "with codes outside the traditional ASCII code range,
 " & _
            "Pi (" & ChrW(928) & ")
 and Sigma (" & ChrW(931) & ")."
        Console.WriteLine("Original string:")
        Console.WriteLine(unicodeString)
        
        ' Encode the string.
        Dim encodedBytes As Byte()
 = uni.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
 = uni.GetString(encodedBytes)
        Console.WriteLine()
        Console.WriteLine("Decoded bytes:")
        Console.WriteLine(decodedString)
    End Sub
End Class
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main()
 {
        // The encoding.
        UnicodeEncoding unicode = new UnicodeEncoding();
        
        // Create a string that contains Unicode characters.
        String unicodeString =
            "This Unicode string contains two characters
 " +
            "with codes outside the traditional ASCII code range, " +
            "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Encode the string.
        Byte[] encodedBytes = unicode.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 = unicode.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
    }
}
using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   
   // The encoding.
   UnicodeEncoding^ unicode = gcnew UnicodeEncoding;
   
   // Create a String* that contains Unicode characters.
   String^ unicodeString = L"This Unicode string contains
 two characters with codes outside the traditional ASCII code range, Pi (\u03a0)
 and Sigma (\u03a3).";
   Console::WriteLine( "Original string:" );
   Console::WriteLine( unicodeString );
   
   // Encode the String*.
   array<Byte>^encodedBytes = unicode->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 = unicode->GetString( encodedBytes );
   Console::WriteLine();
   Console::WriteLine( "Decoded bytes:" );
   Console::WriteLine( decodedString );
}

import System.*;
import System.Text.*;

class UnicodeEncodingExample
{
    public static void main(String[]
 args)
    {
        // The encoding.
        UnicodeEncoding unicode = new UnicodeEncoding();

        // Create a string that contains Unicode characters.
        String unicodeString = "This Unicode string contains
 two characters " 
            + "with codes outside the traditional ASCII code range, " 
            + "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Encode the string.
        ubyte encodedBytes[] = unicode.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 = unicode.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
    } //main
} //UnicodeEncodingExample
継承階層継承階層
System.Object
   System.Text.Encoding
    System.Text.UnicodeEncoding
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnicodeEncoding メンバ
System.Text 名前空間
Encoding クラス
Encoder クラス
Decoder クラス
UTF32Encoding
UTF8Encoding
UTF7Encoding
ASCIIEncoding クラス
System.Globalization.UnicodeCategory
System.Globalization.CharUnicodeInfo

UnicodeEncoding コンストラクタ ()

UnicodeEncoding クラスの新しいインスタンスを初期化します。

名前空間: System.Text
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Dim instance As New UnicodeEncoding
public UnicodeEncoding ()
public:
UnicodeEncoding ()
public UnicodeEncoding ()
public function UnicodeEncoding ()
解説解説

このコンストラクタは、リトル エンディアン バイト順を使用し、Unicode バイト順マークを付加し、無効なエンコーディングを検出したときに例外をスローしないインスタンスを作成します。

注意に関するメモ注意

セキュリティ上の理由から、throwOnInvalidBytes パラメータを受け取るコンストラクタを使用して、このパラメータを true に設定することにより、エラー検出を有効にすることをお勧めします。

エンコーダでは、最上位バイトが先頭に配置されるビッグ エンディアン バイト順、または最下位バイトが先頭に配置されるリトル エンディアン バイト順が使用されます。たとえば、Latin の大文字 A (コード ポイント U+0041) は次のように 16 進数でシリアル化されます。

UnicodeEncoding は、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、次のように 16 進数でシリアル化されます。

通常、ネイティブなバイト順で Unicode 文字を格納した方が効率的です。たとえば、Intel のコンピュータなど、リトル エンディアンのプラットフォームでは、リトル エンディアンのバイト順を使用した方が効率的です。

バイト順とバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。

使用例使用例

次のコード例は、新しい UnicodeEncoding インスタンスを作成してエンコード名を表示する方法を示しています。

Imports System
Imports System.Text

Class UnicodeEncodingExample

    Public Shared Sub Main()
        Dim uni As New UnicodeEncoding()
        Dim encodingName As String
 = uni.EncodingName
        Console.WriteLine("Encoding name: " &
 encodingName)
    End Sub 'Main
End Class 'UnicodeEncodingExample
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main()
 {
        UnicodeEncoding unicode = new UnicodeEncoding();
        String encodingName = unicode.EncodingName;
        Console.WriteLine("Encoding name: " + encodingName);
    }
}
using namespace System;
using namespace System::Text;
int main()
{
   UnicodeEncoding^ unicode = gcnew UnicodeEncoding;
   String^ encodingName = unicode->EncodingName;
   Console::WriteLine( "Encoding name: {0}", encodingName );
}

import System.*;
import System.Text.*;

class UnicodeEncodingExample
{
    public static void main(String[]
 args)
    {
        UnicodeEncoding unicode = new UnicodeEncoding();
        String encodingName = unicode.get_EncodingName();
        Console.WriteLine(("Encoding name: " + encodingName));
    } //main
} //UnicodeEncodingExample
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

UnicodeEncoding コンストラクタ (Boolean, Boolean)

UnicodeEncoding クラスの新しいインスタンスを初期化します。パラメータでは、ビッグ エンディアン バイト順を使用するかどうか、および Unicode バイト順マークを付加するかどうかを指定します。

名前空間: System.Text
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

このコンストラクタは、無効なエンコーディングを検出したときに例外をスローしないインスタンスを作成します。

注意に関するメモ注意

セキュリティ上の理由から、throwOnInvalidBytes パラメータを受け取るコンストラクタを使用して、このパラメータを true に設定することにより、エラー検出を有効にすることをお勧めします。

エンコーダでは、最上位バイトが先頭に配置されるビッグ エンディアン バイト順、または最下位バイトが先頭に配置されるリトル エンディアン バイト順が使用されます。たとえば、Latin の大文字 A (コード ポイント U+0041) は次のように 16 進数でシリアル化されます。

UnicodeEncoding は、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、次のように 16 進数でシリアル化されます。

通常、ネイティブなバイト順で Unicode 文字を格納した方が効率的です。たとえば、Intel のコンピュータなど、リトル エンディアンのプラットフォームでは、リトル エンディアンのバイト順を使用した方が効率的です。

バイト順とバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。

使用例使用例

次のコード例は、リトル エンディアンまたはビッグ エンディアンのどちらのバイト順をサポートするか、および Unicode バイト順マークをサポートするかどうかを指定して、新しい UnicodeEncoding インスタンスを作成する方法を示しています。UnicodeEncoding インスタンスの動作は、bigEndian パラメータと byteOrderMark パラメータの値によって異なります。

Imports System
Imports System.Text

Class UnicodeEncodingExample
    
    Public Shared Sub Main()

        ' Create a UnicodeEncoding without parameters.
        Dim unicodeDefault As New
 UnicodeEncoding()

        ' Create a UnicodeEncoding to support little-endian byte ordering
        ' and include the Unicode byte order mark.        
        Dim unicodeLittleEndianBOM As New
 UnicodeEncoding(False, True)
        ' Compare this UnicodeEncoding to the UnicodeEncoding without
 parameters.
        DescribeEquivalence(unicodeDefault.Equals(unicodeLittleEndianBOM))
        
        ' Create a UnicodeEncoding to support little-endian byte ordering
        ' and not include the Unicode byte order mark.
        Dim unicodeLittleEndianNoBOM As New
 UnicodeEncoding(False, False)
        ' Compare this UnicodeEncoding to the UnicodeEncoding without
 parameters.
        DescribeEquivalence(unicodeDefault.Equals(unicodeLittleEndianNoBOM))
        
        ' Create a UnicodeEncoding to support big-endian byte ordering
        ' and include the Unicode byte order mark.
        Dim unicodeBigEndianBOM As New
 UnicodeEncoding(True, True)
        ' Compare this UnicodeEncoding to the UnicodeEncoding without
 parameters.
        DescribeEquivalence(unicodeDefault.Equals(unicodeBigEndianBOM))
        
        ' Create a UnicodeEncoding to support big-endian byte ordering
        ' and not include the Unicode byte order mark.
        Dim unicodeBigEndianNoBOM As New
 UnicodeEncoding(True, False)
        ' Compare this UnicodeEncoding to the UnicodeEncoding without
 parameters.
        DescribeEquivalence(unicodeDefault.Equals(unicodeBigEndianNoBOM))
    End Sub
    
    
    Public Shared Sub DescribeEquivalence(isEquivalent
 As Boolean)
        Dim phrase as String
        If isEquivalent Then
            phrase = "An"
        Else
            phrase = "Not an"
        End If
        Console.WriteLine("{0} equivalent encoding.",
 phrase)
    End Sub
End Class
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main()
 {

        // Create a UnicodeEncoding without parameters.
        UnicodeEncoding unicode = new UnicodeEncoding();

        // Create a UnicodeEncoding to support little-endian byte ordering
        // and include the Unicode byte order mark.
        UnicodeEncoding unicodeLittleEndianBOM = 
            new UnicodeEncoding(false, true);
        // Compare this UnicodeEncoding to the UnicodeEncoding without
 parameters.
        DescribeEquivalence(unicode.Equals(unicodeLittleEndianBOM));

        // Create a UnicodeEncoding to support little-endian byte ordering
        // and not include the Unicode byte order mark.
        UnicodeEncoding unicodeLittleEndianNoBOM =
            new UnicodeEncoding(false, false);
        // Compare this UnicodeEncoding to the UnicodeEncoding without
 parameters.
        DescribeEquivalence(unicode.Equals(unicodeLittleEndianNoBOM));

        // Create a UnicodeEncoding to support big-endian byte ordering
        // and include the Unicode byte order mark.
        UnicodeEncoding unicodeBigEndianBOM =
            new UnicodeEncoding(true, true);
        // Compare this UnicodeEncoding to the UnicodeEncoding without
 parameters.
        DescribeEquivalence(unicode.Equals(unicodeBigEndianBOM));

        // Create a UnicodeEncoding to support big-endian byte ordering
        // and not include the Unicode byte order mark.
        UnicodeEncoding unicodeBigEndianNoBOM =
            new UnicodeEncoding(true, false);
        // Compare this UnicodeEncoding to the UnicodeEncoding without
 parameters.
        DescribeEquivalence(unicode.Equals(unicodeBigEndianNoBOM));
    }

    public static void DescribeEquivalence(Boolean
 isEquivalent) {
        Console.WriteLine(
            "{0} equivalent encoding.", (isEquivalent ? "An"
 : "Not an")
        );
    }
}
using namespace System;
using namespace System::Text;
void DescribeEquivalence( Boolean isEquivalent )
{
   Console::WriteLine( " {0} equivalent encoding.", (isEquivalent ? (String^)"An"
 : "Not an") );
}

int main()
{
   
   // Create a UnicodeEncoding without parameters.
   UnicodeEncoding^ unicode = gcnew UnicodeEncoding;
   
   // Create a UnicodeEncoding to support little-endian Byte ordering
   // and include the Unicode Byte order mark.
   UnicodeEncoding^ unicodeLittleEndianBOM = gcnew UnicodeEncoding( false,true
 );
   
   // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
   DescribeEquivalence( unicode->Equals( unicodeLittleEndianBOM ) );
   
   // Create a UnicodeEncoding to support little-endian Byte ordering
   // and not include the Unicode Byte order mark.
   UnicodeEncoding^ unicodeLittleEndianNoBOM = gcnew UnicodeEncoding( false,false
 );
   
   // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
   DescribeEquivalence( unicode->Equals( unicodeLittleEndianNoBOM ) );
   
   // Create a UnicodeEncoding to support big-endian Byte ordering
   // and include the Unicode Byte order mark.
   UnicodeEncoding^ unicodeBigEndianBOM = gcnew UnicodeEncoding( true,true
 );
   
   // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
   DescribeEquivalence( unicode->Equals( unicodeBigEndianBOM ) );
   
   // Create a UnicodeEncoding to support big-endian Byte ordering
   // and not include the Unicode Byte order mark.
   UnicodeEncoding^ unicodeBigEndianNoBOM = gcnew UnicodeEncoding( true,false
 );
   
   // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
   DescribeEquivalence( unicode->Equals( unicodeBigEndianNoBOM ) );
}

import System.*;
import System.Text.*;
import System.Boolean;

class UnicodeEncodingExample
{
    public static void main(String[]
 args)
    {
        // Create a UnicodeEncoding without parameters.
        UnicodeEncoding unicode = new UnicodeEncoding();

        // Create a UnicodeEncoding to support little-endian byte ordering
        // and include the Unicode byte order mark.
        UnicodeEncoding unicodeLittleEndianBOM = new 
            UnicodeEncoding(false, true);

        // Compare this UnicodeEncoding to the
        // UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeLittleEndianBOM));

        // Create a UnicodeEncoding to support little-endian byte ordering
        // and not include the Unicode byte order mark.
        UnicodeEncoding unicodeLittleEndianNoBOM = new 
            UnicodeEncoding(false, false);

        // Compare this UnicodeEncoding to the UnicodeEncoding 
        // without parameters.
        DescribeEquivalence(unicode.Equals(unicodeLittleEndianNoBOM));

        // Create a UnicodeEncoding to support big-endian byte ordering
        // and include the Unicode byte order mark.
        UnicodeEncoding unicodeBigEndianBOM = new UnicodeEncoding(true,
 true);

        // Compare this UnicodeEncoding to the
        // UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeBigEndianBOM));

        // Create a UnicodeEncoding to support big-endian byte ordering
        // and not include the Unicode byte order mark.
        UnicodeEncoding unicodeBigEndianNoBOM = new 
            UnicodeEncoding(true, false);

        // Compare this UnicodeEncoding to the
        // UnicodeEncoding without parameters.
        DescribeEquivalence(unicode.Equals(unicodeBigEndianNoBOM));
    } //main

    public static void DescribeEquivalence(boolean
 isEquivalent)
    {
        Console.WriteLine("{0} equivalent encoding.",
            (isEquivalent) ? "An" : "Not an");
    } //DescribeEquivalence
} //UnicodeEncodingExample
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnicodeEncoding クラス
UnicodeEncoding メンバ
System.Text 名前空間
GetPreamble

UnicodeEncoding コンストラクタ (Boolean, Boolean, Boolean)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

UnicodeEncoding クラスの新しいインスタンスを初期化します。パラメータでは、ビッグ エンディアン バイト順を使用するかどうか、Unicode バイト順マークを付加するかどうか、および無効なエンコーディングを検出したときに例外をスローするかどうかを指定します。

名前空間: System.Text
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

throwOnInvalidBytes パラメータが true の場合、無効なバイト シーケンスを検出したメソッドは System.ArgumentException をスローします。それ以外の場合は、例外をスローせず、無効なシーケンスを無視します。

注意に関するメモ注意

セキュリティ上の理由から、このコンストラクタを使用して UnicodeEncoding クラスのインスタンスを作成し、throwOnInvalidBytes を true に設定することでエラー検出をオンにすることをお勧めします。

エンコーダでは、最上位バイトが先頭に配置されるビッグ エンディアン バイト順、または最下位バイトが先頭に配置されるリトル エンディアン バイト順が使用されます。たとえば、Latin の大文字 A (コード ポイント U+0041) は次のように 16 進数でシリアル化されます。

UnicodeEncoding クラスは、オプションでプリアンブルを提供します。プリアンブルは、エンコーディング プロセスで得られたバイト シーケンスの先頭に付加できるバイトの配列です。プリアンブルにバイト順マーク (コード ポイント U+FEFF) が含まれる場合、デコーダはバイト順および変換形式 (UTF) を判断できます。Unicode バイト順マークは、次のように 16 進数でシリアル化されます。

通常、ネイティブなバイト順で Unicode 文字を格納した方が効率的です。たとえば、Intel のコンピュータなど、リトル エンディアンのプラットフォームでは、リトル エンディアンのバイト順を使用した方が効率的です。

バイト順とバイト順マークの詳細については、www.unicode.org の「Unicode Standard」を参照してください。

使用例使用例

次のコード例は、エラー検出を有効にした場合と無効にした場合の UnicodeEncoding の動作を示します。

Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Public Class SamplesUnicodeEncoding   

   Public Shared Sub Main()

      ' Create an instance of UnicodeEncoding using little-endian byte
 order.
      ' This will be used for encoding.
      Dim u16LE As New UnicodeEncoding(False,
 True)

      ' Create two instances of UnicodeEncoding using big-endian byte
 order: one with error detection and one without.
      ' These will be used for decoding.
      Dim u16withED As New
 UnicodeEncoding(True, True, True)
      Dim u16noED As New
 UnicodeEncoding(True, True, False)

      ' Create byte arrays from the same string containing the following
 characters:
      '    Latin Small Letter Z (U+007A)
      '    Latin Small Letter A (U+0061)
      '    Combining Breve (U+0306)
      '    Latin Small Letter AE With Acute (U+01FD)
      '    Greek Small Letter Beta (U+03B2)
      '    Latin Capital Letter U with  Diaeresis (U+00DC)
      Dim myStr As String
 = "za" & ChrW(&H0306) & ChrW(&H01FD)
 & ChrW(&H03B2) & ChrW(&H00DC)

      ' Encode the string using little-endian byte order.
      Dim myBytes(u16LE.GetByteCount(myStr)) As
 Byte
      u16LE.GetBytes(myStr, 0, myStr.Length, myBytes, 0)

      ' Decode the byte array with error detection.
      Console.WriteLine("Decoding with error detection:")
      PrintDecodedString(myBytes, u16withED)

      ' Decode the byte array without error detection.
      Console.WriteLine("Decoding without error detection:")
      PrintDecodedString(myBytes, u16noED)

   End Sub 'Main


   ' Decode the bytes and display the string.
   Public Shared Sub PrintDecodedString(bytes()
 As Byte, enc As Encoding)

      Try
         Console.WriteLine("   Decoded string: {0}",
 enc.GetString(bytes, 0, bytes.Length))
      Catch e As System.ArgumentException
         Console.WriteLine(e.ToString())
      End Try

      Console.WriteLine()

   End Sub 'PrintDecodedString
 

End Class 'SamplesUnicodeEncoding

using System;
using System.Text;

public class SamplesUnicodeEncoding  {

   public static void Main()
  {

      // Create an instance of UnicodeEncoding using little-endian byte
 order.
      // This will be used for encoding.
      UnicodeEncoding u16LE     = new UnicodeEncoding( false,
 true );

      // Create two instances of UnicodeEncoding using big-endian byte
 order: one with error detection and one without.
      // These will be used for decoding.
      UnicodeEncoding u16withED = new UnicodeEncoding( true,
 true, true );
      UnicodeEncoding u16noED   = new UnicodeEncoding( true,
 true, false );

      // Create byte arrays from the same string containing the following
 characters:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      //    Latin Capital Letter U with  Diaeresis (U+00DC)
      String myStr = "za\u0306\u01FD\u03B2\u00DC";

      // Encode the string using little-endian byte order.
      byte[] myBytes = new byte[u16LE.GetByteCount( myStr )];
      u16LE.GetBytes( myStr, 0, myStr.Length, myBytes, 0 );

      // Decode the byte array with error detection.
      Console.WriteLine( "Decoding with error detection:" );
      PrintDecodedString( myBytes, u16withED );

      // Decode the byte array without error detection.
      Console.WriteLine( "Decoding without error detection:" );
      PrintDecodedString( myBytes, u16noED );

   }


   // Decode the bytes and display the string.
   public static void PrintDecodedString(
 byte[] bytes, Encoding enc )  {

      try  {
         Console.WriteLine( "   Decoded string: {0}",
 enc.GetString( bytes, 0, bytes.Length ) );
      }
      catch ( System.ArgumentException e )  {
         Console.WriteLine( e.ToString() );
      }

      Console.WriteLine();

   }

}

using namespace System;
using namespace System::Text;
void PrintDecodedString( array<Byte>^bytes, Encoding^ enc
 );
int main()
{
   
   // Create an instance of UnicodeEncoding using little-endian byte
 order.
   // This will be used for encoding.
   UnicodeEncoding^ u16LE = gcnew UnicodeEncoding( false,true
 );
   
   // Create two instances of UnicodeEncoding using big-endian byte
 order: one with error detection and one without.
   // These will be used for decoding.
   UnicodeEncoding^ u16withED = gcnew UnicodeEncoding( true,true,true
 );
   UnicodeEncoding^ u16noED = gcnew UnicodeEncoding( true,true,false
 );
   
   // Create byte arrays from the same string containing the following
 characters:
   //    Latin Small Letter Z (U+007A)
   //    Latin Small Letter A (U+0061)
   //    Combining Breve (U+0306)
   //    Latin Small Letter AE With Acute (U+01FD)
   //    Greek Small Letter Beta (U+03B2)
   //    Latin Capital Letter U with  Diaeresis (U+00DC)
   String^ myStr = "za\u0306\u01FD\u03B2\u00DC";
   
   // Encode the string using little-endian byte order.
   array<Byte>^myBytes = gcnew array<Byte>(u16LE->GetByteCount( myStr
 ));
   u16LE->GetBytes( myStr, 0, myStr->Length, myBytes, 0 );
   
   // Decode the byte array with error detection.
   Console::WriteLine( "Decoding with error detection:" );
   PrintDecodedString( myBytes, u16withED );
   
   // Decode the byte array without error detection.
   Console::WriteLine( "Decoding without error detection:" );
   PrintDecodedString( myBytes, u16noED );
}


// Decode the bytes and display the string.
void PrintDecodedString( array<Byte>^bytes, Encoding^ enc
 )
{
   try
   {
      Console::WriteLine( "   Decoded string: {0}",
 enc->GetString( bytes, 0, bytes->Length ) );
   }
   catch ( System::ArgumentException^ e ) 
   {
      Console::WriteLine( e );
   }

   Console::WriteLine();
}

import System.*;
import System.Text.*;

public class SamplesUnicodeEncoding
{
    public static void main(String[]
 args)
    {
        // Create an instance of UnicodeEncoding using little-endian
 
        // byte order.
        // This will be used for encoding.
        UnicodeEncoding u16LE = new UnicodeEncoding(false,
 true);

        // Create two instances of UnicodeEncoding using big-endian
 byte 
        // order: one with error detection and one without.
        // These will be used for decoding.
        UnicodeEncoding u16withED = new UnicodeEncoding(true,
 true, true);
        UnicodeEncoding u16noED = new UnicodeEncoding(true,
 true, false);

        // Create byte arrays from the same string containing 
        // the following characters:
        //    Latin Small Letter Z (U+007A)
        //    Latin Small Letter A (U+0061)
        //    Combining Breve (U+0306)
        //    Latin Small Letter AE With Acute (U+01FD)
        //    Greek Small Letter Beta (U+03B2)
        //    Latin Capital Letter U with  Diaeresis (U+00DC)
        String myStr = "za\u0306\u01FD\u03B2\u00DC";

        // Encode the string using little-endian byte order.
        ubyte myBytes[] = new ubyte[u16LE.GetByteCount(myStr)];
        u16LE.GetBytes(myStr, 0, myStr.get_Length(), myBytes, 0);

        // Decode the byte array with error detection.
        Console.WriteLine("Decoding with error detection:");
        PrintDecodedString(myBytes, u16withED);

        // Decode the byte array without error detection.
        Console.WriteLine("Decoding without error detection:");
        PrintDecodedString(myBytes, u16noED);
    } //main

    // Decode the bytes and display the string.
    public static void PrintDecodedString(ubyte[]
 bytes, Encoding enc)
    {
        try {
            Console.WriteLine("   Decoded string: {0}",
 
                enc.GetString(bytes, 0, bytes.length));
        }
        catch(System.ArgumentException e) {
            Console.WriteLine(e.toString());
        }
        Console.WriteLine();
    } //PrintDecodedString
} //SamplesUnicodeEncoding

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnicodeEncoding クラス
UnicodeEncoding メンバ
System.Text 名前空間
GetPreamble

UnicodeEncoding コンストラクタ


UnicodeEncoding フィールド


パブリック フィールドパブリック フィールド

参照参照

関連項目

UnicodeEncoding クラス
System.Text 名前空間
Encoding クラス
Encoder クラス
Decoder クラス
UTF32Encoding
UTF8Encoding
UTF7Encoding
ASCIIEncoding クラス
System.Globalization.UnicodeCategory
System.Globalization.CharUnicodeInfo

UnicodeEncoding プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ ASCII  ASCII (7 ビット) 文字セットのエンコーディングを取得します。 ( Encoding から継承されます。)
パブリック プロパティ BigEndianUnicode  ビッグ エンディアン バイト順を使用する UTF-16 形式のエンコーディングを取得します。 ( Encoding から継承されます。)
パブリック プロパティ BodyName  派生クラスでオーバーライドされた場合、メール エージェントの Body タグと共に使用できる現在のエンコーディングの名前を取得します。 ( Encoding から継承されます。)
パブリック プロパティ CodePage  派生クラスでオーバーライドされた場合、現在の Encoding のコード ページ ID を取得します。 ( Encoding から継承されます。)
パブリック プロパティ DecoderFallback  現在の Encoding オブジェクトの DecoderFallback オブジェクトを取得または設定します。 ( Encoding から継承されます。)
パブリック プロパティ Default  システムの現在の ANSI コード ページのエンコーディングを取得します。 ( Encoding から継承されます。)
パブリック プロパティ EncoderFallback  現在の Encoding オブジェクトの EncoderFallback オブジェクトを取得または設定します。 ( Encoding から継承されます。)
パブリック プロパティ EncodingName  派生クラスでオーバーライドされた場合、現在のエンコーディングについての記述を、ユーザーが判読できる形式で取得します。 ( Encoding から継承されます。)
パブリック プロパティ HeaderName  派生クラスでオーバーライドされた場合、メール エージェント ヘッダー タグと共に使用できる現在のエンコーディングの名前を取得します。 ( Encoding から継承されます。)
パブリック プロパティ IsBrowserDisplay  派生クラスでオーバーライドされた場合、ブラウザ クライアントが現在のエンコーディングを使用してコンテンツを表示できるかどうかを示す値を取得します。 ( Encoding から継承されます。)
パブリック プロパティ IsBrowserSave  派生クラスでオーバーライドされた場合、ブラウザ クライアントが現在のエンコーディングを使用してコンテンツを保存できるかどうかを示す値を取得します。 ( Encoding から継承されます。)
パブリック プロパティ IsMailNewsDisplay  派生クラスでオーバーライドされた場合、メール クライアントおよびニュース クライアントが現在のエンコーディングを使用してコンテンツを表示できるかどうかを示す値を取得します。 ( Encoding から継承されます。)
パブリック プロパティ IsMailNewsSave  派生クラスでオーバーライドされた場合、メール クライアントおよびニュース クライアントが現在のエンコーディングを使用してコンテンツを保存できるかどうかを示す値を取得します。 ( Encoding から継承されます。)
パブリック プロパティ IsReadOnly  派生クラスでオーバーライドされた場合、現在のエンコーディングが読み取り専用かどうかを示す値を取得します。 ( Encoding から継承されます。)
パブリック プロパティ IsSingleByte  派生クラスでオーバーライドされた場合、現在のエンコーディングが 1 バイトのコード ポイントを使用するかどうかを示す値を取得します。 ( Encoding から継承されます。)
パブリック プロパティ Unicode  リトル エンディアン バイト順を使用する UTF-16 形式のエンコーディングを取得します。 ( Encoding から継承されます。)
パブリック プロパティ UTF32  リトル エンディアン バイト順を使用する UTF-32 形式のエンコーディングを取得します。 ( Encoding から継承されます。)
パブリック プロパティ UTF7  UTF-7 形式のエンコーディングを取得します。 ( Encoding から継承されます。)
パブリック プロパティ UTF8  UTF-8 形式のエンコーディングを取得します。 ( Encoding から継承されます。)
パブリック プロパティ WebName  派生クラスでオーバーライドされた場合、現在のエンコーディングの IANA (Internet Assigned Numbers Authority) に登録されている名前を取得します。 ( Encoding から継承されます。)
パブリック プロパティ WindowsCodePage  派生クラスでオーバーライドされた場合、現在のエンコーディングに最も厳密に対応する Windows オペレーティング システムのコード ページを取得します。 ( Encoding から継承されます。)
参照参照

関連項目

UnicodeEncoding クラス
System.Text 名前空間
Encoding クラス
Encoder クラス
Decoder クラス
UTF32Encoding
UTF8Encoding
UTF7Encoding
ASCIIEncoding クラス
System.Globalization.UnicodeCategory
System.Globalization.CharUnicodeInfo

UnicodeEncoding メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Clone  派生クラスでオーバーライドされた場合、現在の Encoding オブジェクトの簡易コピーを作成します。 ( Encoding から継承されます。)
パブリック メソッド Convert  オーバーロードされます。 バイト配列を、あるエンコーディングから別のエンコーディングに変換します。 ( Encoding から継承されます。)
パブリック メソッド Equals オーバーロードされます。 オーバーライドされます。  
パブリック メソッド GetByteCount オーバーロードされます。 文字のセットをエンコードすることによって生成されるバイト数を計算します。
パブリック メソッド GetBytes オーバーロードされます。 文字のセットをバイト シーケンスにエンコードします。
パブリック メソッド GetCharCount オーバーロードされます。 バイト シーケンスをデコードすることによって生成される文字数を計算します。
パブリック メソッド GetChars オーバーロードされます。 バイト シーケンスを文字のセットにデコードします。
パブリック メソッド GetDecoder オーバーライドされます。 UTF-32 でエンコードされたバイト シーケンスを Unicode 文字のシーケンスに変換するデコーダを取得します。
パブリック メソッド GetEncoder オーバーライドされます。 Unicode 文字のシーケンスを UTF-32 でエンコードされたバイト シーケンスに変換するエンコーダを取得します。
パブリック メソッド GetEncoding  オーバーロードされます。 指定したコード ページのエンコーディングを返します。 ( Encoding から継承されます。)
パブリック メソッド GetEncodings  すべてのエンコーディングを格納する配列を返します。 ( Encoding から継承されます。)
パブリック メソッド GetHashCode オーバーライドされます。 現在のインスタンスのハッシュ コードを返します。
パブリック メソッド GetMaxByteCount オーバーライドされます。 指定した文字数をエンコードすることによって生成される最大バイト数を計算します。
パブリック メソッド GetMaxCharCount オーバーライドされます。 指定したバイト数をデコードすることによって生成される最大文字数を計算します。
パブリック メソッド GetPreamble オーバーライドされます。 このインスタンスのコンストラクタでバイト順マークを付加するよう指定した場合に、UTF-32 形式でエンコードされた Unicode バイト順マークを返します。
パブリック メソッド GetString オーバーロードされます。 バイト シーケンスを文字列にデコードします。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド IsAlwaysNormalized  オーバーロードされます。 現在のエンコーディングが常に正規化されるかどうかを示す値を取得します。 ( Encoding から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

UnicodeEncoding クラス
System.Text 名前空間
Encoding クラス
Encoder クラス
Decoder クラス
UTF32Encoding
UTF8Encoding
UTF7Encoding
ASCIIEncoding クラス
System.Globalization.UnicodeCategory
System.Globalization.CharUnicodeInfo

UnicodeEncoding メンバ

Unicode 文字の UTF-16 エンコーディングを表します。

UnicodeEncoding データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック フィールドパブリック フィールド
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ ASCII  ASCII (7 ビット) 文字セットのエンコーディングを取得します。(Encoding から継承されます。)
パブリック プロパティ BigEndianUnicode  ビッグ エンディアン バイト順を使用する UTF-16 形式のエンコーディングを取得します。(Encoding から継承されます。)
パブリック プロパティ BodyName  派生クラスでオーバーライドされた場合、メール エージェントの Body タグと共に使用できる現在のエンコーディングの名前を取得します。(Encoding から継承されます。)
パブリック プロパティ CodePage  派生クラスでオーバーライドされた場合、現在の Encoding のコード ページ ID を取得します。(Encoding から継承されます。)
パブリック プロパティ DecoderFallback  現在の Encoding オブジェクトの DecoderFallback オブジェクトを取得または設定します。(Encoding から継承されます。)
パブリック プロパティ Default  システムの現在の ANSI コード ページのエンコーディングを取得します。(Encoding から継承されます。)
パブリック プロパティ EncoderFallback  現在の Encoding オブジェクトの EncoderFallback オブジェクトを取得または設定します。(Encoding から継承されます。)
パブリック プロパティ EncodingName  派生クラスでオーバーライドされた場合、現在のエンコーディングについての記述を、ユーザーが判読できる形式で取得します。(Encoding から継承されます。)
パブリック プロパティ HeaderName  派生クラスでオーバーライドされた場合、メール エージェント ヘッダー タグと共に使用できる現在のエンコーディングの名前を取得します。(Encoding から継承されます。)
パブリック プロパティ IsBrowserDisplay  派生クラスでオーバーライドされた場合、ブラウザ クライアントが現在のエンコーディングを使用してコンテンツを表示できるかどうかを示す値を取得します。(Encoding から継承されます。)
パブリック プロパティ IsBrowserSave  派生クラスでオーバーライドされた場合、ブラウザ クライアントが現在のエンコーディングを使用してコンテンツを保存できるかどうかを示す値を取得します。(Encoding から継承されます。)
パブリック プロパティ IsMailNewsDisplay  派生クラスでオーバーライドされた場合、メール クライアントおよびニュース クライアントが現在のエンコーディングを使用してコンテンツを表示できるかどうかを示す値を取得します。(Encoding から継承されます。)
パブリック プロパティ IsMailNewsSave  派生クラスでオーバーライドされた場合、メール クライアントおよびニュース クライアントが現在のエンコーディングを使用してコンテンツを保存できるかどうかを示す値を取得します。(Encoding から継承されます。)
パブリック プロパティ IsReadOnly  派生クラスでオーバーライドされた場合、現在のエンコーディングが読み取り専用かどうかを示す値を取得します。(Encoding から継承されます。)
パブリック プロパティ IsSingleByte  派生クラスでオーバーライドされた場合、現在のエンコーディングが 1 バイトのコード ポイントを使用するかどうかを示す値を取得します。(Encoding から継承されます。)
パブリック プロパティ Unicode  リトル エンディアン バイト順を使用する UTF-16 形式のエンコーディングを取得します。(Encoding から継承されます。)
パブリック プロパティ UTF32  リトル エンディアン バイト順を使用する UTF-32 形式のエンコーディングを取得します。(Encoding から継承されます。)
パブリック プロパティ UTF7  UTF-7 形式のエンコーディングを取得します。(Encoding から継承されます。)
パブリック プロパティ UTF8  UTF-8 形式のエンコーディングを取得します。(Encoding から継承されます。)
パブリック プロパティ WebName  派生クラスでオーバーライドされた場合、現在のエンコーディングの IANA (Internet Assigned Numbers Authority) に登録されている名前を取得します。(Encoding から継承されます。)
パブリック プロパティ WindowsCodePage  派生クラスでオーバーライドされた場合、現在のエンコーディングに最も厳密に対応する Windows オペレーティング システムのコード ページを取得します。(Encoding から継承されます。)
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Clone  派生クラスでオーバーライドされた場合、現在の Encoding オブジェクトの簡易コピーを作成します。 (Encoding から継承されます。)
パブリック メソッド Convert  オーバーロードされます。 バイト配列を、あるエンコーディングから別のエンコーディングに変換します。 (Encoding から継承されます。)
パブリック メソッド Equals オーバーロードされます。 オーバーライドされます。  
パブリック メソッド GetByteCount オーバーロードされます。 文字のセットをエンコードすることによって生成されるバイト数を計算します。
パブリック メソッド GetBytes オーバーロードされます。 文字のセットをバイト シーケンスにエンコードします。
パブリック メソッド GetCharCount オーバーロードされます。 バイト シーケンスをデコードすることによって生成される文字数を計算します。
パブリック メソッド GetChars オーバーロードされます。 バイト シーケンスを文字のセットにデコードします。
パブリック メソッド GetDecoder オーバーライドされます。 UTF-32 でエンコードされたバイト シーケンスを Unicode 文字のシーケンスに変換するデコーダを取得します。
パブリック メソッド GetEncoder オーバーライドされます。 Unicode 文字のシーケンスを UTF-32 でエンコードされたバイト シーケンスに変換するエンコーダを取得します。
パブリック メソッド GetEncoding  オーバーロードされます。 指定したコード ページのエンコーディングを返します。 (Encoding から継承されます。)
パブリック メソッド GetEncodings  すべてのエンコーディングを格納する配列を返します。 (Encoding から継承されます。)
パブリック メソッド GetHashCode オーバーライドされます。 現在のインスタンスのハッシュ コードを返します。
パブリック メソッド GetMaxByteCount オーバーライドされます。 指定した文字数をエンコードすることによって生成される最大バイト数を計算します。
パブリック メソッド GetMaxCharCount オーバーライドされます。 指定したバイト数をデコードすることによって生成される最大文字数を計算します。
パブリック メソッド GetPreamble オーバーライドされます。 このインスタンスのコンストラクタでバイト順マークを付加するよう指定した場合に、UTF-32 形式でエンコードされた Unicode バイト順マークを返します。
パブリック メソッド GetString オーバーロードされます。 バイト シーケンスを文字列にデコードします。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド IsAlwaysNormalized  オーバーロードされます。 現在のエンコーディングが常に正規化されるかどうかを示す値を取得します。 (Encoding から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

UnicodeEncoding クラス
System.Text 名前空間
Encoding クラス
Encoder クラス
Decoder クラス
UTF32Encoding
UTF8Encoding
UTF7Encoding
ASCIIEncoding クラス
System.Globalization.UnicodeCategory
System.Globalization.CharUnicodeInfo



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「UnicodeEncoding」の関連用語

UnicodeEncodingのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



UnicodeEncodingのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS