首页 新闻 赞助 找找看

C# 汉字转Unicode

0
悬赏园豆:50 [已解决问题] 解决于 2012-04-14 15:34

在C#中如何将汉字转换成Unicode码

一毛钱的爱的主页 一毛钱的爱 | 初学一级 | 园豆:168
提问于:2012-04-13 11:21
< >
分享
最佳答案
0

试试这个

public static string GBToUnicode(string text) {

  byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text);

  string lowCode = "", temp = "";

   for (int i = 0; i < bytes.Length; i++) {

       if (i % 2 == 0) {

            temp = System.Convert.ToString(bytes[i], 16);//取出元素4编码内容(两位16进制)

           if (temp.Length < 2) temp = "0" + temp;

        } else {

            string mytemp = Convert.ToString(bytes[i], 16);

          if (mytemp.Length < 2) mytemp = "0" + mytemp;

          lowCode = lowCode + @"\u" + mytemp + temp;//取出元素4编码内容(两位16进制)

        }

        }

       return lowCode;

}

收获园豆:35
嘉华 | 初学一级 |园豆:106 | 2012-04-13 11:27
其他回答(3)
0
string s = "";
byte[] encoded = Encoding.Unicode.GetBytes(s);

如果想用其它编码方式(比如UTF8编码)只需要改成Encoding.UTF8.GetBytes就可以了。

收获园豆:5
水牛刀刀 | 园豆:6350 (大侠五级) | 2012-04-13 11:30
0

关注。。。

收获园豆:5
KivenRo | 园豆:1734 (小虾三级) | 2012-04-13 12:55
0
myChar是你要转的汉字:
Console.WriteLine("U+{0:x4}",(int)myChar);
收获园豆:5
zsounder | 园豆:2819 (老鸟四级) | 2012-04-13 13:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册