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");
}
}
}
在.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");
}
}
}
本文介绍如何在.NET框架中使用ASCIIEncoding类进行ASCII与UTF-16编码之间的转换。通过实例展示了如何将UTF-16字符串转换为ASCII码,以及如何将ASCII码转换回对应的字符。

4396

被折叠的 条评论
为什么被折叠?



