zoukankan      html  css  js  c++  java
  • 使用 Unicode 编码

    面向公共语言执行库的应用程序使用编码将字符表示形式从本机字符方案(Unicode)映射为其它方案。

    应用程序使用解码将字符从非本机方案(非 Unicode)映射为本机方案。

    System.Text 命名空间提供了使您可以对字符进行编码和解码的类。System.Text 编码支持包含下面编码:

    Unicode UTF-32 编码

    Unicode UTF-32 编码将 Unicode 字符表示为 32 位整数序列。您能够使用 UTF32Encoding 类在字符和 UTF-32 编码之间相互转换。

    Unicode UTF-16 编码

    Unicode UTF-16 编码将 Unicode 字符表示为 16 位整数序列。您能够使用 UnicodeEncoding 类在字符和 UTF-16 编码之间相互转换。

    Unicode UTF-8 编码

    Unicode UTF-8 编码将 Unicode 字符表示为 8 位字节序列。您能够使用 UTF8Encoding 类在字符和 UTF-8 编码之间相互转换。

    Unicode UTF-7 编码

    Unicode UTF-7 编码将 Unicode 字符表示为 7 位字节 ASCII 字符的序列。

    非 ASCII Unicode 字符由 ASCII 字符的转义序列表示。

    UTF-7 编码是为支持某些须要 UTF-7 编码的协议而存在的,这些协议一般是电子邮件或新闻组协议。可是,UTF-7 并不是特别安全或可靠。在某些情况下,更改一个位可能会全然改变对整个 UTF-7 字符串的解释。在其它情况下,差点儿全然不同的 UTF-7 字符串能够对同样的文本进行编码。此外,对于包括非 ASCII 字符的序列,UTF-7 的空间利用效率比 UTF-8 低非常多,并且编码/解码的速度较慢。

    因此。假设有其它选择,通常不应使用 UTF-7:UTF-8 通常应优先于 UTF-7。

    能够使用 UTF7Encoding 类在字符和 UTF-7 编码之间相互转换。

    ASCII 编码

    ASCII 编码将拉丁字母编码为单个 7 位 ASCII 字符。因为此编码仅支持从 U+0000 到 U+007F 的值,因此大多数情况下,对于国际化应用程序来说,它是不够的。您能够使用 ASCIIEncoding 类在字符和 ASCII 编码之间相互转换。有关在代码中使用 ASCIIEncoding 类的演示样例,请參见为基类型编码

    ANSI/ISO 编码

    System.Text.Encoding 类对范围较广的 ANSI/ISO 编码提供支持。

    使用 Encoding 类

    您能够使用 Encoding.GetEncoding 方法为指定的编码返回编码对象。

    能够使用 Encoding.GetBytes 方法以指定的编码将 Unicode 字符串转换为它的字节表示形式。

    以下的代码演示样例使用 Encoding.GetEncoding 方法为指定的代码页创建目标编码对象。针对目标编码对象调用 Encoding.GetBytes 方法。可在目标编码中将 Unicode 字符串转换为它的字节表示形式。字符串的字节表示形式随即以指定的代码页显示出来。

    using System;
    using System.IO;
    using System.Globalization;
    using System.Text;
    
    public class Encoding_UnicodeToCP
    {
       public static void Main()
       {
          // Converts ASCII characters to bytes.
          // Displays the string's byte representation in the 
          // specified code page.
          // Code page 1252 represents Latin characters.
          PrintCPBytes("Hello, World!",1252);
          // Code page 932 represents Japanese characters.
          PrintCPBytes("Hello, World!",932);
    
          // Converts Japanese characters to bytes.
          PrintCPBytes("u307b,u308b,u305a,u3042,u306d",1252);
          PrintCPBytes("u307b,u308b,u305a,u3042,u306d",932);
       }
    
       public static void PrintCPBytes(string str, int codePage)
       {
          Encoding targetEncoding;
          byte[] encodedChars;
    
          // Gets the encoding for the specified code page.
          targetEncoding = Encoding.GetEncoding(codePage);
    
          // Gets the byte representation of the specified string.
          encodedChars = targetEncoding.GetBytes(str);
    
          // Prints the bytes.
          Console.WriteLine
                   ("Byte representation of '{0}' in Code Page  '{1}':", str, 
                      codePage);
          for (int i = 0; i < encodedChars.Length; i++)
                   Console.WriteLine("Byte {0}: {1}", i, encodedChars[i]);
       }
    }
    
    Note注意

    假设您在控制台应用程序中执行此代码。则指定的 Unicode 文本元素可能会显示不对,由于控制台环境中对 Unicode 字符的支持会因所执行的 Windows 操作系统的版本号而异。

    在 ASP.NET 应用程序中能够使用这些方法来确定用于响应字符的编码。将 HttpResponse.ContentEncoding 属性的值设置为由适当的方法返回的值。以下的代码演示样例阐释了怎样设置 HttpResponse.ContentEncoding

    // Explicitly sets the encoding to UTF-8.
    Response.ContentEncoding = Encoding.UTF8;
    
    // Sets ContentEncoding using the name of an encoding.
    Response.ContentEncoding = Encoding.GetEncoding(name);
    
    // Sets ContentEncoding using a code page number.
    Response.ContentEncoding = Encoding.GetEncoding(codepageNumber);
    

    对于大多数 ASP.NET 应用程序,应使 HttpResponse.ContentEncoding 属性与 HttpRequest.ContentEncoding 属性相匹配,以便在编码中显示client期望的文本。

    有关在 ASP.NET 中使用编码的很多其它信息,请參见常见任务高速入门中的“多种编码演示样例”和 ASP.NET 高速入门中的“设置区域性和编码演示样例”。

  • 相关阅读:
    linux中bin和sbin目录的主要区别
    C# 值类型 引用类型 作为参数传递区别
    绿色免安装电视直播软件viviplayer
    [转]中科大校长建议停止以行政主导的高校评估
    MDS 7.0 使用中的问题 2(数据交换中图元的丢失)
    怪异的慢递公司一统快递
    [转]全国最好的医院列表
    多普达D600 问题集锦
    推荐PDG阅读器UnicornViewer
    [转]《乒乓世界》封面故事:中国男乒直板三人行
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6803265.html
Copyright © 2011-2022 走看看