首先复习一下计算机基础知识:
计算机中最底层的数据都是用二进制及0和1来表示的。每个0或1称作1位,第8位二进制数叫做1个字节,它可以表示ASCII码中的一个字符。中文计算机中用两个字节即16位二进制来表示一个汉字。而在Unicode编码中所有的符号(包括汉字,英文,标题及其它众多符号)都是为两字节(16)位来表示。
在System.Text命名空间中包含众多编码的类,可供进行操作及转换,下面用两个实例来进行区位码及汉字之间的互换,希望能起到举一反三的效果,让大家可以轻松处理文字编码方面的问题:
usingSystem;
usingSystem.Text;
classCodingChange
{
publicstringCharacterToCoding(stringcharacter)
{
stringcoding="";
for(inti=0;i<character.Length;i++)
{
byte[]bytes=System.Text.Encoding.Unicode.GetBytes(character.Substring(i,1));//取出二进制编码内容
stringlowCode=System.Convert.ToString(bytes[0],16);//取出低字节编码内容(两位16进制)
if(lowCode.Length==1)
lowCode="0"+lowCode;
stringhightCode=System.Convert.ToString(bytes[1],16);//取出高字节编码内容(两位16进制)
if(hightCode.Length==1)
hightCode="0"+hightCode;
coding+=(lowCode+hightCode);//加入到字符串中,
}
returncoding;
}
publicstringCodingToCharacter(stringcoding)
{
stringcharacters="";
转自: http://www.guoblog.com/blogview.asp?logID=201
计算机中最底层的数据都是用二进制及0和1来表示的。每个0或1称作1位,第8位二进制数叫做1个字节,它可以表示ASCII码中的一个字符。中文计算机中用两个字节即16位二进制来表示一个汉字。而在Unicode编码中所有的符号(包括汉字,英文,标题及其它众多符号)都是为两字节(16)位来表示。
在System.Text命名空间中包含众多编码的类,可供进行操作及转换,下面用两个实例来进行区位码及汉字之间的互换,希望能起到举一反三的效果,让大家可以轻松处理文字编码方面的问题:
| 程序代码: |
usingSystem;
usingSystem.Text;
classCodingChange
{
publicstringCharacterToCoding(stringcharacter)
{
stringcoding="";
for(inti=0;i<character.Length;i++)
{
byte[]bytes=System.Text.Encoding.Unicode.GetBytes(character.Substring(i,1));//取出二进制编码内容
stringlowCode=System.Convert.ToString(bytes[0],16);//取出低字节编码内容(两位16进制)
if(lowCode.Length==1)
lowCode="0"+lowCode;
stringhightCode=System.Convert.ToString(bytes[1],16);//取出高字节编码内容(两位16进制)
if(hightCode.Length==1)
hightCode="0"+hightCode;
coding+=(lowCode+hightCode);//加入到字符串中,
}
returncoding;
}
publicstringCodingToCharacter(stringcoding)
{
stringcharacters="";
转自: http://www.guoblog.com/blogview.asp?logID=201

1213

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



