Encoding.UTF8 Encoding.UTF8 Encoding.UTF8 Encoding.UTF8 Property

Definition

Ruft eine Codierung für das UTF-8-Format ab.Gets an encoding for the UTF-8 format.

public:
 static property System::Text::Encoding ^ UTF8 { System::Text::Encoding ^ get(); };
public static System.Text.Encoding UTF8 { get; }
member this.UTF8 : System.Text.Encoding
Public Shared ReadOnly Property UTF8 As Encoding

Eigenschaftswert

Eine Codierung für das UTF-8-Format.An encoding for the UTF-8 format.

Beispiele

Das folgende Beispiel definiert ein Array, das aus der folgenden Zeichen:The following example defines an array that consists of the following characters:

  • KLEINES LATEINISCHES Z (U + 007A)LATIN SMALL LETTER Z (U+007A)

  • LATEINISCHE KLEINE BUCHSTABEN EINER (U + 0061 IST)LATIN SMALL LETTER A (U+0061)

  • KOMBINIEREN VON BREVIS (U + 0306)COMBINING BREVE (U+0306)

  • KLEINES LATEINISCHES AE MIT AKUTE (U + 01FD)LATIN SMALL LETTER AE WITH ACUTE (U+01FD)

  • GRIECHISCHE BUCHSTABE BETA (U + 03B2)GREEK SMALL LETTER BETA (U+03B2)

  • Ein Ersatzzeichenpaar (U + D800 U + DD54), die griechischen ACROPHONIC ATTIC eine TAUSEND STATERS (U + 10154) bildet.A surrogate pair (U+D800 U+DD54) that forms GREEK ACROPHONIC ATTIC ONE THOUSAND STATERS (U+10154).

Es zeigt die UTF-16-Codeeinheiten der einzelnen Zeichen und bestimmt die Anzahl der Bytes, die durch einen UTF-8-Encoder zum Codieren der Zeichen erforderlich.It displays the UTF-16 code units of each character and determines the number of bytes required by a UTF-8 encoder to encode the character array. Anschließend werden die Zeichen codiert und zeigt die resultierende UTF-8-codierten Bytes.It then encodes the characters and displays the resulting UTF-8-encoded bytes.

using System;
using System.Text;

public class Example
{
   public static void Main()  
   {
      // Create a character array.
      string gkNumber = Char.ConvertFromUtf32(0x10154);
      char[] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', 
                                  gkNumber[0], gkNumber[1] };

      // Get UTF-8 and UTF-16 encoders.
      Encoding utf8 = Encoding.UTF8;
      Encoding utf16 = Encoding.Unicode;
      
      // Display the original characters' code units.
      Console.WriteLine("Original UTF-16 code units:");
      byte[] utf16Bytes = utf16.GetBytes(chars);
      foreach (var utf16Byte in utf16Bytes)
         Console.Write("{0:X2} ", utf16Byte);
      Console.WriteLine();
         
      // Display the number of bytes required to encode the array.
      int reqBytes  = utf8.GetByteCount(chars);
      Console.WriteLine("\nExact number of bytes required: {0}", 
                    reqBytes);

      // Display the maximum byte count.
      int maxBytes = utf8.GetMaxByteCount(chars.Length);
      Console.WriteLine("Maximum number of bytes required: {0}\n", 
                        maxBytes);

      // Encode the array of chars.
      byte[] utf8Bytes = utf8.GetBytes(chars);

      // Display all the UTF-8-encoded bytes.
      Console.WriteLine("UTF-8-encoded code units:");
      foreach (var utf8Byte in utf8Bytes)
         Console.Write("{0:X2} ", utf8Byte);
      Console.WriteLine();
   }
}
// The example displays the following output:
//       Original UTF-16 code units:
//       7A 00 61 00 06 03 FD 01 B2 03 00 D8 54 DD
//       
//       Exact number of bytes required: 12
//       Maximum number of bytes required: 24
//       
//       UTF-8-encoded code units:
//       7A 61 CC 86 C7 BD CE B2 F0 90 85 94
Imports System.Text

Public Module Example
   Public Sub Main()
      ' Create a character array.
      Dim gkNumber As String = Char.ConvertFromUtf32(&h10154)
      Dim chars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), 
                             ChrW(&H03B2), gkNumber(0), gkNumber(1) }
 
      ' Get UTF-8 and UTF-16 encoders.
      Dim utf8 As Encoding = Encoding.UTF8
      Dim utf16 As Encoding = Encoding.Unicode

      ' Display the original characters' code units.
      Console.WriteLine("Original UTF-16 code units:")
      Dim utf16Bytes() As Byte = utf16.GetBytes(chars)
      For Each utf16Byte In utf16Bytes
         Console.Write("{0:X2} ", utf16Byte)
      Next
      Console.WriteLine()

      Console.WriteLine()
      ' Display the number of bytes required to encode the array.
      Dim reqBytes As Integer = utf8.GetByteCount(chars)
      Console.WriteLine("Exact number of bytes required: {0}", 
                        reqBytes)

      ' Display the maximum byte count.
      Dim maxBytes As Integer = utf8.GetMaxByteCount(chars.Length)
      Console.WriteLine("Maximum number of bytes required: {0}", 
                        maxBytes)
      Console.WriteLine()
      
      ' Encode the array of characters.
      Dim utf8Bytes() As Byte = utf8.GetBytes(chars)

      ' Display all the UTF-8-encoded bytes.
      Console.WriteLine("UTF-8-encoded code units:")
      For Each utf8Byte In utf8Bytes
         Console.Write("{0:X2} ", utf8Byte)
      Next
      Console.WriteLine()
   End Sub 
End Module 
' The example displays the following output:
'    Original UTF-16 code units:
'    7A 00 61 00 06 03 FD 01 B2 03 00 D8 54 DD
'    
'    Exact number of bytes required: 12
'    Maximum number of bytes required: 24
'    
'    UTF-8-encoded code units:
'    7A 61 CC 86 C7 BD CE B2 F0 90 85 94

Hinweise

Diese Eigenschaft gibt eine UTF8Encoding Objekt, das Unicode (UTF-16-codierte)-Zeichen in eine Folge von ein bis vier Bytes pro Zeichen codiert und, die ein UTF-8-codierte Bytearray in Unicode (UTF-16-codierte) Zeichen decodiert.This property returns a UTF8Encoding object that encodes Unicode (UTF-16-encoded) characters into a sequence of one to four bytes per character, and that decodes a UTF-8-encoded byte array to Unicode (UTF-16-encoded) characters. Weitere Informationen dazu, die zeichencodierungen, die von .NET und eine Beschreibung der um zu verwendende Codierung Unicode-Zeichen unterstützt werden, finden Sie unter Zeichencodierung in .NET.For information about the character encodings supported by .NET and a discussion of which Unicode encoding to use, see Character Encoding in .NET.

Die UTF8Encoding -Objekt, das von dieser Eigenschaft zurückgegeben wird möglicherweise nicht das richtige Verhalten für Ihre app.The UTF8Encoding object that is returned by this property might not have the appropriate behavior for your app.

  • Gibt eine UTF8Encoding Objekt, das eine Unicode-bytereihenfolgemarkierung (BOM) bereitstellt.It returns a UTF8Encoding object that provides a Unicode byte order mark (BOM). Rufen Sie Überladung der zum Instanziieren einer UTF8-Codierung, die eine Bytereihenfolge-Marke bereitstellen, nicht die UTF8Encoding Konstruktor.To instantiate a UTF8 encoding that doesn't provide a BOM, call any overload of the UTF8Encoding constructor.

  • Gibt eine UTF8Encoding -Objekt, das verwendet einen Ersatzfallback, ersetzen Sie jede Zeichenfolge, die nicht codiert werden können und jedes Byte, die nicht decodiert durch ein Fragezeichen ("?") Zeichen.It returns a UTF8Encoding object that uses replacement fallback to replace each string that it can't encode and each byte that it can't decode with a question mark ("?") character. Stattdessen rufen Sie die UTF8Encoding.UTF8Encoding(Boolean, Boolean) Konstruktor zum Instanziieren einer UTF8Encoding -Objekt, dessen Fallback entweder ist, ein EncoderFallbackException oder ein DecoderFallbackException, wie im folgende Beispiel veranschaulicht.Instead, you can call the UTF8Encoding.UTF8Encoding(Boolean, Boolean) constructor to instantiate a UTF8Encoding object whose fallback is either an EncoderFallbackException or a DecoderFallbackException, as the following example illustrates.

    using System;
    using System.Text;
    
    public class Example
    {
       public static void Main()
       {
          Encoding enc = new UTF8Encoding(true, true);
          string value = "\u00C4 \uD802\u0033 \u00AE"; 
    
          try {
             byte[] bytes= enc.GetBytes(value);
             foreach (var byt in bytes)
                Console.Write("{0:X2} ", byt);
             Console.WriteLine();
    
             string value2 = enc.GetString(bytes);
             Console.WriteLine(value2);
          }
          catch (EncoderFallbackException e) {
             Console.WriteLine("Unable to encode {0} at index {1}", 
                               e.IsUnknownSurrogate() ? 
                                  String.Format("U+{0:X4} U+{1:X4}", 
                                                Convert.ToUInt16(e.CharUnknownHigh),
                                                Convert.ToUInt16(e.CharUnknownLow)) :
                                  String.Format("U+{0:X4}", 
                                                Convert.ToUInt16(e.CharUnknown)),
                               e.Index);
          }                     
       }
    }
    // The example displays the following output:
    //        Unable to encode U+D802 at index 2
    
    Imports System.Text
    
    Module Example
       Public Sub Main()
          Dim enc As Encoding = New UTF8Encoding(True, True)
          Dim value As String = String.Format("{0} {1}{2} {3}", 
                                ChrW(&h00C4), ChrW(&hD802), ChrW(&h0033), ChrW(&h00AE))
          
          Try
             Dim bytes() As Byte = enc.GetBytes(value)
             For Each byt As Byte In bytes
                Console.Write("{0:X2} ", byt)
             Next       
             Console.WriteLine()
             Dim value2 As String = enc.GetString(bytes)
             Console.WriteLine(value2)
          Catch e As EncoderFallbackException
             Console.WriteLine("Unable to encode {0} at index {1}", 
                               If(e.IsUnknownSurrogate(), 
                                  String.Format("U+{0:X4} U+{1:X4}", 
                                                Convert.ToUInt16(e.CharUnknownHigh),
                                                Convert.ToUInt16(e.CharUnknownLow)),
                                  String.Format("U+{0:X4}", 
                                                Convert.ToUInt16(e.CharUnknown))),
                               e.Index)
          End Try
       End Sub
    End Module
    ' The example displays the following output:
    '       Unable to encode U+D802 at index 2
    

Gilt für:

Siehe auch