當前位置: 首頁>>代碼示例>>C#>>正文


C# Encoding.UTF8屬性代碼示例

本文整理匯總了C#中System.Text.Encoding.UTF8屬性的典型用法代碼示例。如果您正苦於以下問題:C# Encoding.UTF8屬性的具體用法?C# Encoding.UTF8怎麽用?C# Encoding.UTF8使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在System.Text.Encoding的用法示例。


在下文中一共展示了Encoding.UTF8屬性的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
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();
   }
}
開發者ID:.NET開發者,項目名稱:System.Text,代碼行數:44,代碼來源:Encoding.UTF8

輸出:

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

示例2: Main

//引入命名空間
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);
      }                     
   }
}
開發者ID:.NET開發者,項目名稱:System.Text,代碼行數:32,代碼來源:Encoding.UTF8

輸出:

Unable to encode U+D802 at index 2

示例3: Main

//引入命名空間
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main() 
    {        
        using (StreamWriter output = new StreamWriter("practice.txt")) 
        {
            // Create and write a string containing the symbol for Pi.
            string srcString = "Area = \u03A0r^2";

            // Convert the UTF-16 encoded source string to UTF-8 and ASCII.
            byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
            byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
            
            // Write the UTF-8 and ASCII encoded byte arrays. 
            output.WriteLine("UTF-8  Bytes: {0}", BitConverter.ToString(utf8String));
            output.WriteLine("ASCII  Bytes: {0}", BitConverter.ToString(asciiString));
            
            Console.WriteLine(BitConverter.ToString(utf8String));
            Console.WriteLine(BitConverter.ToString(asciiString));
        }
    }
}
開發者ID:C#程序員,項目名稱:System.Text,代碼行數:27,代碼來源:Encoding.UTF8


注:本文中的System.Text.Encoding.UTF8屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。