UnicodeEncoding Konstruktorok

Definíció

Inicializálja a UnicodeEncoding osztály új példányát.

Túlterhelések

Name Description
UnicodeEncoding()

Inicializálja a UnicodeEncoding osztály új példányát.

UnicodeEncoding(Boolean, Boolean)

Inicializálja a UnicodeEncoding osztály új példányát. A paraméterek megadják, hogy a big endian bájtsorrendet kell-e használni, és hogy a GetPreamble() metódus Unicode bájtsorrendjelet ad-e vissza.

UnicodeEncoding(Boolean, Boolean, Boolean)

Inicializálja a UnicodeEncoding osztály új példányát. A paraméterek meghatározzák, hogy a big endian bájtsorrendet használja-e, hogy Unicode bájtsorrendjelet adjon-e meg, és hogy kivételt adjon-e ki, ha érvénytelen kódolást észlel.

UnicodeEncoding()

Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs

Inicializálja a UnicodeEncoding osztály új példányát.

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

Példák

Az alábbi példa bemutatja, hogyan hozhat létre új UnicodeEncoding példányt, és hogyan jelenítheti meg a kódolás nevét.

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

Megjegyzések

Ez a konstruktor létrehoz egy példányt, amely a kis endian bájtsorrendet használja, Unicode bájtsorrendjelet biztosít, és nem ad kivételt érvénytelen kódolás észlelésekor.

Figyelmeztetés

Biztonsági okokból engedélyeznie kell a hibaészlelést a UnicodeEncoding(Boolean, Boolean, Boolean) konstruktor meghívásával és argumentumának throwOnInvalidBytesbeállításávaltrue.

A következőre érvényes:

UnicodeEncoding(Boolean, Boolean)

Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs

Inicializálja a UnicodeEncoding osztály új példányát. A paraméterek megadják, hogy a big endian bájtsorrendet kell-e használni, és hogy a GetPreamble() metódus Unicode bájtsorrendjelet ad-e vissza.

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)

Paraméterek

bigEndian
Boolean

true a big endian bájtsorrend (a legjelentősebb bájt első) vagy false a kis endian bájtsorrend (a legkisebb jelentős bájt első) használatára.

byteOrderMark
Boolean

true annak megadásához, hogy a GetPreamble() metódus Unicode bájtsorrendjelet ad vissza; ellenkező esetben false.

Példák

Az alábbi példa bemutatja, hogyan hozhat létre új UnicodeEncoding példányt, amely meghatározza, hogy támogatja-e a kis endian vagy big endian bájtsorrendezést és a Unicode bájtsorrendjelet.

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

Megjegyzések

Ez a konstruktor létrehoz egy példányt, amely nem okoz kivételt érvénytelen kódolás észlelésekor.

Figyelmeztetés

Biztonsági okokból engedélyeznie kell a hibaészlelést a UnicodeEncoding(Boolean, Boolean, Boolean) konstruktor meghívásával és argumentumának throwOnInvalidBytesbeállításávaltrue.

A byteOrderMark paraméter vezérli a GetPreamble metódus működését. Ha true, a metódus egy bájttömböt ad vissza, amely a Unicode bájtsorrend-jelet (BOM) tartalmazza UTF-16 formátumban. Ha false, akkor nulla hosszúságú bájttömböt ad vissza. A beállítás byteOrderMarktrue azonban nem eredményezi, hogy a GetBytes metódus előtagot adjon a BOM-nak a bájttömb elején, és azt sem, hogy a GetByteCount metódus belefoglalja a bájtok számát a BOM-ban a bájtok számába.

Lásd még

A következőre érvényes:

UnicodeEncoding(Boolean, Boolean, Boolean)

Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs
Forrás:
UnicodeEncoding.cs

Inicializálja a UnicodeEncoding osztály új példányát. A paraméterek meghatározzák, hogy a big endian bájtsorrendet használja-e, hogy Unicode bájtsorrendjelet adjon-e meg, és hogy kivételt adjon-e ki, ha érvénytelen kódolást észlel.

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)

Paraméterek

bigEndian
Boolean

true a big endian bájtsorrend (a legjelentősebb bájt első) használata; false a kis endian bájtsorrend (a legkevésbé jelentős bájt első) használatára.

byteOrderMark
Boolean

true annak megadásához, hogy a GetPreamble() metódus Unicode bájtsorrendjelet ad vissza; ellenkező esetben false.

throwOnInvalidBytes
Boolean

trueannak meghatározása, hogy a kódolás érvénytelen észlelésekor kivételt kell-e eredményezni; egyéb esetben. false

Példák

Az alábbi példa a hibaészlelés engedélyezett és anélküli viselkedését UnicodeEncodingmutatja be.

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

Megjegyzések

A byteOrderMark paraméter vezérli a GetPreamble metódus működését. Ha true, a metódus egy bájttömböt ad vissza, amely a Unicode bájtsorrend-jelet (BOM) tartalmazza UTF-16 formátumban. Ha false, akkor nulla hosszúságú bájttömböt ad vissza. A beállítás byteOrderMarktrue azonban nem eredményezi, hogy a GetBytes metódus előtagot adjon a BOM-nak a bájttömb elején, és azt sem, hogy a GetByteCount metódus belefoglalja a bájtok számát a BOM-ban a bájtok számába.

Ha a throwOnInvalidBytes paraméter az true, akkor érvénytelen bájtütemezést észlelő metódus dob System.ArgumentException. Ellenkező esetben a metódus nem ad kivételt, és a rendszer figyelmen kívül hagyja az érvénytelen sorozatot.

Figyelmeztetés

Biztonsági okokból ezt a konstruktort használva hozza létre az osztály egy példányát, és kapcsolja be a UnicodeEncoding hibaészlelést a throwOnInvalidByteskövetkező beállítássaltrue: .

Lásd még

A következőre érvényes: