Szerkesztés

Megosztás a következőn keresztül:


UnicodeEncoding Constructors

Definition

Initializes a new instance of the UnicodeEncoding class.

Overloads

UnicodeEncoding()

Initializes a new instance of the UnicodeEncoding class.

UnicodeEncoding(Boolean, Boolean)

Initializes a new instance of the UnicodeEncoding class. Parameters specify whether to use the big endian byte order and whether the GetPreamble() method returns a Unicode byte order mark.

UnicodeEncoding(Boolean, Boolean, Boolean)

Initializes a new instance of the UnicodeEncoding class. Parameters specify whether to use the big endian byte order, whether to provide a Unicode byte order mark, and whether to throw an exception when an invalid encoding is detected.

UnicodeEncoding()

Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs

Initializes a new instance of the UnicodeEncoding class.

public:
 UnicodeEncoding();
public UnicodeEncoding ();
Public Sub New ()

Examples

The following example demonstrates how to create a new UnicodeEncoding instance and display the name of the encoding.

using namespace System;
using namespace System::Text;
int main()
{
   UnicodeEncoding^ unicode = gcnew UnicodeEncoding;
   String^ encodingName = unicode->EncodingName;
   Console::WriteLine( "Encoding name: {0}", encodingName );
}
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        UnicodeEncoding unicode = new UnicodeEncoding();
        String encodingName = unicode.EncodingName;
        Console.WriteLine("Encoding name: " + encodingName);
    }
}
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
End Class

Remarks

This constructor creates an instance that uses the little endian byte order, provides a Unicode byte order mark, and does not throw an exception when an invalid encoding is detected.

Caution

For security reasons, you should enable error detection by calling the UnicodeEncoding(Boolean, Boolean, Boolean) constructor and setting its throwOnInvalidBytes argument to true.

Applies to

UnicodeEncoding(Boolean, Boolean)

Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs

Initializes a new instance of the UnicodeEncoding class. Parameters specify whether to use the big endian byte order and whether the GetPreamble() method returns a Unicode byte order mark.

public:
 UnicodeEncoding(bool bigEndian, bool byteOrderMark);
public UnicodeEncoding (bool bigEndian, bool byteOrderMark);
new System.Text.UnicodeEncoding : bool * bool -> System.Text.UnicodeEncoding
Public Sub New (bigEndian As Boolean, byteOrderMark As Boolean)

Parameters

bigEndian
Boolean

true to use the big endian byte order (most significant byte first), or false to use the little endian byte order (least significant byte first).

byteOrderMark
Boolean

true to specify that the GetPreamble() method returns a Unicode byte order mark; otherwise, false.

Examples

The following example demonstrates how to create a new UnicodeEncoding instance specifying whether to support little endian or big endian byte ordering and the Unicode byte order mark.

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 ) );
}
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")
        );
    }
}
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

Remarks

This constructor creates an instance that does not throw an exception when an invalid encoding is detected.

Caution

For security reasons, you should enable error detection by calling the UnicodeEncoding(Boolean, Boolean, Boolean) constructor and setting its throwOnInvalidBytes argument to true.

The byteOrderMark parameter controls the operation of the GetPreamble method. If true, the method returns a byte array containing the Unicode byte order mark (BOM) in UTF-16 format. If false, it returns a zero-length byte array. However, setting byteOrderMark to true does not cause the GetBytes method to prefix the BOM at the beginning of the byte array, nor does it cause the GetByteCount method to include the number of bytes in the BOM in the byte count.

See also

Applies to

UnicodeEncoding(Boolean, Boolean, Boolean)

Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs

Initializes a new instance of the UnicodeEncoding class. Parameters specify whether to use the big endian byte order, whether to provide a Unicode byte order mark, and whether to throw an exception when an invalid encoding is detected.

public:
 UnicodeEncoding(bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes);
public UnicodeEncoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes);
new System.Text.UnicodeEncoding : bool * bool * bool -> System.Text.UnicodeEncoding
Public Sub New (bigEndian As Boolean, byteOrderMark As Boolean, throwOnInvalidBytes As Boolean)

Parameters

bigEndian
Boolean

true to use the big endian byte order (most significant byte first); false to use the little endian byte order (least significant byte first).

byteOrderMark
Boolean

true to specify that the GetPreamble() method returns a Unicode byte order mark; otherwise, false.

throwOnInvalidBytes
Boolean

true to specify that an exception should be thrown when an invalid encoding is detected; otherwise, false.

Examples

The following example demonstrates the behavior of UnicodeEncoding, both with error detection enabled and without.

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();
}
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();
   }
}
Imports System.Text

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


   ' 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

End Class

Remarks

The byteOrderMark parameter controls the operation of the GetPreamble method. If true, the method returns a byte array containing the Unicode byte order mark (BOM) in UTF-16 format. If false, it returns a zero-length byte array. However, setting byteOrderMark to true does not cause the GetBytes method to prefix the BOM at the beginning of the byte array, nor does it cause the GetByteCount method to include the number of bytes in the BOM in the byte count.

If the throwOnInvalidBytes parameter is true, a method that detects an invalid byte sequence throws System.ArgumentException. Otherwise, the method does not throw an exception, and the invalid sequence is ignored.

Caution

For security reasons, you should use this constructor to create an instance of the UnicodeEncoding class and turn on error detection by setting throwOnInvalidBytes to true.

See also

Applies to