最近手上在做一个项目,涉及到了编码问题,查了不少资料。下面内容是关于C#中中文和字符串转成ASCII码和ASCII码转成字符的代码
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
static class Module1
{
public static int ConvertToAscii(char c)
{
return Strings.AscW(c);
}
public static char ConvertFromAscii(int n)
{
return Strings.ChrW(n);
}
public static void Main()
{
//测试
string s = "我爱北京天安门";
foreach (char c in s) {
Console.Write(ConvertToAscii(c).ToString() + " ");
}
Console.WriteLine();
int[] n = new int[] {
25105,
29233,
21271,
22825,
23433,
38376
};
foreach (int num in n) {
Console.Write(ConvertFromAscii(num).ToString() + " ");
}
}
}