最近手上在做一个项目,涉及到了编码问题,查了不少资料。下面内容是关于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() + " ");
}
}
}

本文介绍了一种使用C#进行中文字符与ASCII码相互转换的方法。通过具体示例展示了如何将中文字符串转换为ASCII码,以及如何将ASCII码转换回对应的中文字符。

1万+

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



