作者toki (いまがその时だ)
看板C_Sharp
标题Re: [问题] 请问:要如何让字元转换成ASCII Code?
时间Thu Nov 6 18:06:04 2008
※ 引述《a030164851 (小加号)》之铭言:
: 因为老师说要让字串里面的字元依ASCII Code排序,
: 想让字元直接比大小系统不让实作(只有相等可以比),
: 但是找不到方法可以让字元转换成ASCII Code!
: 不知道能不能告诉我要怎麽转换?
: 找过网路跟资料库,可是多数都是C++的转换!
:
: --
:
※ 发信站: 批踢踢实业坊(ptt.cc)
: ◆ From: 125.224.118.146
: → carl71321:字元转int即可 11/06 12:25
: → a030164851:使用转int也不能比较其大小,大概是因为我用矩阵去存那 11/06 14:48
转成 int 就是转成字元的内码,而且都转成 int 了还不能比大小?
不过直接转 int 确实不是 ASCII code (是 Unicode),但至少英文的部分是一样的
: → a030164851:些字元,所以可能还是必需转成ASCII Code来试试看!! 11/06 14:49
下面这程式 demo 了两种转法的不同
对照一下就可以发现,中文的部分在两种模式下转出来的结果是不一样的
static void Main(string[] args)
{
string tmp_str = "This is a test. 中文测试";
Console.WriteLine("Now printing out '{0}'", tmp_str);
Console.WriteLine("Print use int cast.");
foreach (char c in tmp_str)
{
Console.Write("{0} ", (int)c);
}
Console.WriteLine();
Console.WriteLine("Print use Encoding.ASCII.GetBytes()");
byte[] asc_array = Encoding.ASCII.GetBytes(tmp_str);
foreach (byte b in asc_array)
{
Console.Write("{0} ", b);
}
Console.ReadKey();
}
"This is a test. 中文测试" 这个字串转出来的结果
Now printing out 'This is a test. 中文测试'
Print use int cast.
84 104 105 115 32 105 115 32 97 32 116 101 115 116 46 32 20013 25991 28204 35430
Print use Encoding.ASCII.GetBytes()
84 104 105 115 32 105 115 32 97 32 116 101 115 116 46 32 63 63 63 63
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.116.82.44