Encoding.UTF8 属性

定义

获取 UTF-8 格式的编码。

C#
public static System.Text.Encoding UTF8 { get; }

属性值

Encoding

UTF-8 格式的编码。

示例

下面的示例定义一个包含以下字符的数组:

  • 拉丁文小写字母 Z (U + 007A)

  • 拉丁文小写字母 A (U + 0061)

  • 组合短音符(U + 0306)

  • 拉丁文小写字母 AE WITH 锐音符(U + 01FD)

  • 希腊文小写字母 BETA (U + 03B2)

  • 构成希腊语 ACROPHONIC 阁楼 1000 STATERS (U + 10154)的代理项对(U + D800 U + DD54)。

它显示每个字符的 UTF-16 代码单元,并确定 UTF-8 编码器对字符数组进行编码所需的字节数。 然后,它对字符进行编码,并显示生成的 UTF-8 编码字节。

C#
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

注解

此属性返回一个 UTF8Encoding 对象,该对象将 Unicode (utf-16 编码的)字符编码为每个字符一至四个字节的序列,并将 utf-8 编码的字节数组解码为 Unicode (utf-16 编码的)字符。 有关 .NET 支持的字符编码以及要使用的 Unicode 编码的讨论,请参阅.net 中的字符编码

UTF8Encoding此属性返回的对象可能没有适用于你的应用程序的行为。

  • 它将返回一个 UTF8Encoding 对象,该对象提供一个 Unicode 字节顺序标记(BOM)。 若要实例化不提供 BOM 的 UTF8 编码,请调用构造函数的任何重载 UTF8Encoding

  • 它将返回一个 UTF8Encoding 对象,该对象使用替换回退来替换每个无法编码的字符串,以及无法使用问号("?")字符解码的每个字节。 相反,你可以调用 UTF8Encoding.UTF8Encoding(Boolean, Boolean) 构造函数来实例化 UTF8Encoding 其回退为 EncoderFallbackException 或的对象 DecoderFallbackException ,如下例所示。

    C#
    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
    

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

另请参阅