UnicodeEncoding Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy UnicodeEncoding.

Przeciążenia

UnicodeEncoding()

Inicjuje nowe wystąpienie klasy UnicodeEncoding.

UnicodeEncoding(Boolean, Boolean)

Inicjuje nowe wystąpienie klasy UnicodeEncoding. Parametry określają, czy należy użyć kolejności bajtów big endian i czy GetPreamble() metoda zwraca znak kolejności bajtów Unicode.

UnicodeEncoding(Boolean, Boolean, Boolean)

Inicjuje nowe wystąpienie klasy UnicodeEncoding. Parametry określają, czy należy użyć kolejności bajtów big endian, czy podać znak kolejności bajtów Unicode i czy zgłosić wyjątek w przypadku wykrycia nieprawidłowego kodowania.

UnicodeEncoding()

Źródło:
UnicodeEncoding.cs
Źródło:
UnicodeEncoding.cs
Źródło:
UnicodeEncoding.cs

Inicjuje nowe wystąpienie klasy UnicodeEncoding.

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

Przykłady

W poniższym przykładzie pokazano, jak utworzyć nowe UnicodeEncoding wystąpienie i wyświetlić nazwę kodowania.

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

Uwagi

Ten konstruktor tworzy wystąpienie, które używa małej kolejności bajtów endian, dostarcza znak kolejności bajtów Unicode i nie zgłasza wyjątku w przypadku wykrycia nieprawidłowego kodowania.

Przestroga

Ze względów bezpieczeństwa należy włączyć wykrywanie błędów przez wywołanie konstruktora i ustawienie throwOnInvalidBytes jego argumentu UnicodeEncoding(Boolean, Boolean, Boolean) na true.

Dotyczy

UnicodeEncoding(Boolean, Boolean)

Źródło:
UnicodeEncoding.cs
Źródło:
UnicodeEncoding.cs
Źródło:
UnicodeEncoding.cs

Inicjuje nowe wystąpienie klasy UnicodeEncoding. Parametry określają, czy należy użyć kolejności bajtów big endian i czy GetPreamble() metoda zwraca znak kolejności bajtów Unicode.

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)

Parametry

bigEndian
Boolean

true aby użyć kolejności bajtów big endian (najbardziej znaczący bajt pierwszy) lub false użyć małej kolejności bajtów endian (najmniej znaczący bajt pierwszy).

byteOrderMark
Boolean

true aby określić, że GetPreamble() metoda zwraca znak kolejności bajtów Unicode; w przeciwnym razie false.

Przykłady

W poniższym przykładzie pokazano, jak utworzyć nowe UnicodeEncoding wystąpienie, określając, czy ma być obsługiwane porządkowanie bajtów endian lub big endian oraz znak kolejności bajtów Unicode.

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

Uwagi

Ten konstruktor tworzy wystąpienie, które nie zgłasza wyjątku po wykryciu nieprawidłowego kodowania.

Przestroga

Ze względów bezpieczeństwa należy włączyć wykrywanie błędów przez wywołanie konstruktora i ustawienie throwOnInvalidBytes jego argumentu UnicodeEncoding(Boolean, Boolean, Boolean) na true.

Parametr byteOrderMark steruje operacją GetPreamble metody . Jeśli truemetoda zwraca tablicę bajtów zawierającą znak kolejności bajtów Unicode (BOM) w formacie UTF-16. Jeśli falsewartość , zwraca tablicę bajtów o zerowej długości. Jednak ustawienie byteOrderMark wartości nie powodujeGetBytes, że metoda prefiksu BOM na początku tablicy bajtów ani nie powodujeGetByteCount, że metoda uwzględnia liczbę bajtów w BOM w true liczbie bajtów.

Zobacz też

Dotyczy

UnicodeEncoding(Boolean, Boolean, Boolean)

Źródło:
UnicodeEncoding.cs
Źródło:
UnicodeEncoding.cs
Źródło:
UnicodeEncoding.cs

Inicjuje nowe wystąpienie klasy UnicodeEncoding. Parametry określają, czy należy użyć kolejności bajtów big endian, czy podać znak kolejności bajtów Unicode i czy zgłosić wyjątek w przypadku wykrycia nieprawidłowego kodowania.

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)

Parametry

bigEndian
Boolean

true aby użyć kolejności bajtów big endian (najpierw najbardziej znaczący bajt); false aby użyć małej kolejności bajtów endian (najpierw najmniej znaczący bajt).

byteOrderMark
Boolean

true aby określić, że GetPreamble() metoda zwraca znak kolejności bajtów Unicode; w przeciwnym razie false.

throwOnInvalidBytes
Boolean

true aby określić, że w przypadku wykrycia nieprawidłowego kodowania należy zgłosić wyjątek; w przeciwnym razie , false.

Przykłady

W poniższym przykładzie pokazano zachowanie elementu UnicodeEncoding, zarówno z włączonym wykrywaniem błędów, jak i bez.

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

Uwagi

Parametr byteOrderMark steruje operacją GetPreamble metody . Jeśli truemetoda zwraca tablicę bajtów zawierającą znak kolejności bajtów Unicode (BOM) w formacie UTF-16. Jeśli falsewartość , zwraca tablicę bajtów o zerowej długości. Jednak ustawienie byteOrderMark wartości nie powodujeGetBytes, że metoda prefiksu BOM na początku tablicy bajtów ani nie powodujeGetByteCount, że metoda uwzględnia liczbę bajtów w BOM w true liczbie bajtów.

throwOnInvalidBytes Jeśli parametr ma truewartość , metoda, która wykrywa nieprawidłową sekwencję bajtów, zgłasza System.ArgumentExceptionbłąd . W przeciwnym razie metoda nie zgłasza wyjątku, a nieprawidłowa sekwencja jest ignorowana.

Przestroga

Ze względów bezpieczeństwa należy użyć tego konstruktora do utworzenia wystąpienia UnicodeEncoding klasy i włączenia wykrywania błędów, ustawiając wartość truethrowOnInvalidBytes .

Zobacz też

Dotyczy