C#常用类--Encoding类

2009-11-30 22:31

编码是一个将一组 Unicode 字符转换为一个字节序列的过程。解码是一个反向操作过程,即将一个编码字节序列转换为一组 Unicode 字符。

Unicode 标准为所有支持脚本中的每个字符分配一个码位(一个数字)。Unicode 转换格式 (UTF) 是一种码位编码方式。
Unicode 标准 3.2 版使用下列 UTF:

UTF-8,它将每个码位表示为一个由 1 至 4 个字节组成的序列。
UTF-16,它将每个码位表示为一个由 1 至 2 个 16 位整数组成的序列。
UTF-32,它将每个码位表示为一个 32 位整数。

.NET Framework 提供以下 Encoding 类的实现以支持当前 Unicode 编码和其他编码:

ASCIIEncoding 将 Unicode 字符编码为单个 7 位 ASCII 字符。此编码仅支持 U+0000 和 U+007F 之间的字符值。代码页 20127。还可通过 ASCII 属性获得。
UTF7Encoding 使用 UTF-7 编码对 Unicode 字符进行编码。此编码支持所有 Unicode 字符值。代码页 65000。还可通过 UTF7 属性获得。
UTF8Encoding 使用 UTF-8 编码对 Unicode 字符进行编码。此编码支持所有 Unicode 字符值。代码页 65001。还可通过 UTF8 属性获得。
UnicodeEncoding 使用 UTF-16 编码对 Unicode 字符进行编码。支持 Little-Endian(代码页 1200)和 Big-Endian(代码页 1201)字节顺序。还可通过 Unicode 属性和 BigEndianUnicode 属性获得。
UTF32Encoding 使用 UTF-32 编码对 Unicode 字符进行编码。支持 Little-Endian(代码页 65005)和 Big-Endian(代码页 65006)字节顺序。还可通过 UTF32 属性获得。


使用 GetEncoding 方法以获取其他编码。使用 GetEncodings 方法来获取所有编码的列表。

GetByteCount 方法确定将有多少字节对一组 Unicode 字符进行编码,而 GetBytes 方法将执行实际的编码操作。

同样,GetCharCount 方法确定将有多少字符对字节序列进行解码,而 GetChars 方法执行实际的解码。

如果要转换的数据仅存在于连续块(如从流中读取的数据)中,或者数据量很大,需要划分为较小的块,则可使用由某个派生类的 GetDecoder 方法提供的 Decoder 或由派生类的 GetEncoder 方法提供的 Encoder。


GetDecoder,在派生类中重写时,获取一个解码器,该解码器将已编码的字节序列转换为字符序列。
GetEncoder,在派生类中重写时,获取一个解码器,该解码器将 Unicode 字符序列转换为已编码的字节序列。


GetMaxByteCount 在派生类中重写时,计算对指定数目的字符进行编码所产生的最大字节数。
GetMaxCharCount 在派生类中重写时,计算对指定数目的字节进行解码时所产生的最大字符数。

GetString 在派生类中重写时,将一个字节序列解码为一个字符串。
GetEncoding 返回指定代码页的编码。

UTF-16 和 UTF-32 编码器可以使用 Big-Endian 字节顺序(从最高有效字节开始),也可以使用 Little-Endian 字节顺序(从最低有效字节开始)。例如,大写拉丁字母 A (U+0041) 的序列化结果(十六进制)如下所示:

UTF-16 Big-Endian 字节顺序:00 41

UTF-16 Little-Endian 字节顺序:41 00

UTF-32 Big-Endian 字节顺序:00 00 00 41

UTF-32 Little-Endian 字节顺序:41 00 00 00

或者,Encoding 提供一个前导码(即一个字节数组),可以将它作为编码过程中所产生的字节序列的前缀。如果前导码中包含字节顺序标记(在 Unicode 中,码位为 U+FEFF),则它会帮助解码器确定字节顺序和转换格式或 UTF。Unicode 字节顺序标记的序列化结果(十六进制)如下所示:

UTF-8:EF BB BF

UTF-16 Big-Endian 字节顺序:FE FF

UTF-16 Little-Endian 字节顺序:FF FE

UTF-32 Big-Endian 字节顺序:00 00 FE FF

UTF-32 Little-Endian 字节顺序:FF FE 00 00

通常,使用本机字节顺序存储 Unicode 字符的效率更高。例如,在 Little-Endian 平台(如 Intel 计算机)上最好使用 Little-Endian 字节顺序。

GetPreamble 方法返回一个可以包括字节顺序标记的字节数组。如果将此字节数组作为编码流的前缀,将有助于解码器识别所用的编码格式。

有关字节顺序和字节顺序标记的更多信息,请参见 www.unicode.org 上的“The Unicode Standard”(Unicode 标准)部分。


using System;
using System.Text;

public class SamplesEncoding {

   public static void Main() {

      // The characters to encode:
      //    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)
      //    a high-surrogate value (U+D8FF)
      //    a low-surrogate value (U+DCFF)
      char[] myChars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };

      // Get different encodings.
      Encoding u7    = Encoding.UTF7;
      Encoding u8    = Encoding.UTF8;
      Encoding u16LE = Encoding.Unicode;
      Encoding u16BE = Encoding.BigEndianUnicode;
      Encoding u32   = Encoding.UTF32;

      // Encode the entire array, and print out the counts and the resulting bytes.
      PrintCountsAndBytes( myChars, u7 );
      PrintCountsAndBytes( myChars, u8 );
      PrintCountsAndBytes( myChars, u16LE );
      PrintCountsAndBytes( myChars, u16BE );
      PrintCountsAndBytes( myChars, u32 );

   }


   public static void PrintCountsAndBytes( char[] chars, Encoding enc ) {

      // Display the name of the encoding used.
      Console.Write( "{0,-30} :", enc.ToString() );

      // Display the exact byte count.
      int iBC = enc.GetByteCount( chars );
      Console.Write( " {0,-3}", iBC );

      // Display the maximum byte count.
      int iMBC = enc.GetMaxByteCount( chars.Length );
      Console.Write( " {0,-3} :", iMBC );

      // Encode the array of chars.
      byte[] bytes = enc.GetBytes( chars );

      // Display all the encoded bytes.
      PrintHexBytes( bytes );

   }


   public static void PrintHexBytes( byte[] bytes ) {

      if (( bytes == null ) || ( bytes.Length == 0 ))
         Console.WriteLine( "<none>" );
      else {
         for ( int i = 0; i < bytes.Length; i++ )
            Console.Write( "{0:X2} ", bytes[i] );
         Console.WriteLine();
      }

   }

}


/*
This code produces the following output.

System.Text.UTF7Encoding       : 18 23 :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
System.Text.UTF8Encoding       : 12 24 :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding    : 14 16 :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
System.Text.UnicodeEncoding    : 14 16 :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
System.Text.UTF32Encoding      : 24 32 :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值