Translate Into English Coffee & Tea
ASP.NET 中的 AJAX ASP.NET 画图全接触 ASP.NET 与 XML 声明式数据绑定 ASP.NET 网页抓取
C# 基础 C# 类教程 C# 加密 ASP.NET 成员资格 非“自动化”处理 Excel
QuickAjax Ajax 完美的语法高亮器 Silverlight Popfly
通栏广告
C# 编码转换示例
using System;
using System.Text;

namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";

         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         Encoding unicode = Encoding.Unicode;

         // Convert the string into a byte[].
         byte[] unicodeBytes = unicode.GetBytes(unicodeString);

         // Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
           
         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);

         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}

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

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

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

相关文章
文章评论
标题:必填
内容:
文章信息
  • 作者:
  • 来源:MSDN
  • 时间:2008-5-3