3个回答
展开全部
//字符转数字
public static int Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
//ASCII码转字符:
public static string Chr(int asciiCode)
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
public static int Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
//ASCII码转字符:
public static string Chr(int asciiCode)
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
展开全部
直接int就能把char转成值
string s = "Q";
char [] cs = s.ToCharArray();//把string转成一个个的字符
foreach(char c in cs)
{
Console.WriteLine((int)c); //输出的就是字符的键码值
}
string s = "Q";
char [] cs = s.ToCharArray();//把string转成一个个的字符
foreach(char c in cs)
{
Console.WriteLine((int)c); //输出的就是字符的键码值
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string str2 = "SessionN";
byte[] array = System.Text.Encoding.ASCII.GetBytes(str2); //array为对应的ASCII数组
string ASCIIstr2 = null;
for (int i = 0; i < array.Length; i++)
{
int asciicode = (int)(array[i]);
ASCIIstr2 += Convert.ToString(asciicode);//字符串ASCIIstr2 为对应的ASCII字符串
}
byte[] array = System.Text.Encoding.ASCII.GetBytes(str2); //array为对应的ASCII数组
string ASCIIstr2 = null;
for (int i = 0; i < array.Length; i++)
{
int asciicode = (int)(array[i]);
ASCIIstr2 += Convert.ToString(asciicode);//字符串ASCIIstr2 为对应的ASCII字符串
}
参考资料: http://zhidao.baidu.com/question/174953039.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询