码迷,mamicode.com
首页 > Windows程序 > 详细

C#汉字转十六进制

时间:2015-07-23 09:21:18      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

public class chsHex
{
/// <summary>
/// 从汉字转换到16进制
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string GetHexFromChs(string s)
{
if ((s.Length % 2) != 0)
{
s += " ";//空格
//throw new ArgumentException("s is not valid chinese string!");
}

System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");

byte[] bytes = chs.GetBytes(s);

string str = "";

for (int i = 0; i < bytes.Length; i++)
{
str += string.Format("{0:X}", bytes[i]);
}

return str;
}

C#汉字转十六进制

标签:

原文地址:http://www.cnblogs.com/IT-haidong/p/4669373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!