c# ascii转换方法

来源:互联网 发布:python俄罗斯方块 编辑:程序博客网 时间:2024/06/11 02:56

c# ascii转换方法

.NET Framework使用Unicode UTF-16来表示字符。

在.NET中转化ASCII码,请您使用System.Text.ASCIIEncoding类。它提供了GetBytes()和GetChars()来实施转换。当然,您还可以在.NET联机手册中找到其他有用的函数。

下面是一个转换的例子:


////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Text;

namespace ConsoleApplication3
{

class Class1
{
  
   static void Main(string[] args)
   {

    // UTF-16字符串
    string MyString = "Hello World";

    ASCIIEncoding AE1 = new ASCIIEncoding();

    byte[] ByteArray1 = AE1.GetBytes(MyString);

    //打印出ASCII码
    for(int x = 0;x <= ByteArray1.Length - 1; x++)
    {
     Console.Write("{0} ", ByteArray1[x]);
    }


    Console.Write("\n");

    //把ASCII码转化为对应的字符

    ASCIIEncoding AE2 = new ASCIIEncoding();
    byte[] ByteArray2 = {72,101,108,108,111,32,87,111,114,108,100};
    char[] CharArray = AE2.GetChars(ByteArray2);
    for(int x = 0;x <= CharArray.Length - 1; x++)
    {
     Console.Write(CharArray[x]);
    }
   
    Console.Write("\n");

   }
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

希望能对您有所帮助。

-微软全球技术中心


0 0
原创粉丝点击