会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
Robin's Blog
记录 积累 学习 成长
博客园
::
首页
::
博问
::
闪存
::
新随笔
::
联系
::
订阅
::
管理
::
公告
[C#] ASCII码与字符的转换
Code
字符转ASCII码
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.
"
);
}
}
posted on
2009-06-26 10:46
Robin99
阅读(
379
) 评论(
0
)
收藏
举报
刷新页面
返回顶部