使用System.Convert.ToString将字符串转换成utf16进制编码
来源:互联网 发布:韩国的淘宝是哪个网站 编辑:程序博客网 时间:2024/06/05 09:40
我们在编写某些特殊的字符串处理程序的时候,需要获得字符的16进制编码,这个在vb中一般使用asc函数来进行,但是在c#中就没有那么简单。前一阵,为了将url中的汉字转换成utf编码的,我发现可以用System.Convert.ToString这函数来完成这样的操作。
这个函数就是将url中的汉字转换成utf编码:
public string Str2Hex(string strInput)
{
UTF8Encoding utf8=new UTF8Encoding();
string unicodeString=strInput;//strinput就是需要转换的汉字字符串
string str="";
Byte[] encodedBytes=utf8.GetBytes(unicodeString);//转换成数组
for (int i=0;i<(encodedBytes.Length);i++ )
{
str=str+"%25";//因为ReportingService中的特殊要求,需要在每个汉字前增加一个“%25,即%本身的编码”
str=str+System.Convert.ToString(encodedBytes[i],16);//转换成16进制的编码。
}
return str;
}
//这个函数和上面的大同小异,只是直接在汉字中间加上%而不是它的编码
public string StrToHex(string strInput)
{
UTF8Encoding utf8=new UTF8Encoding();
string unicodeString=strInput;
string str="";
Byte[] encodedBytes=utf8.GetBytes(unicodeString);
for (int i=0;i<(encodedBytes.Length);i++ )
{
str=str+"%";
str=str+System.Convert.ToString(encodedBytes[i],16);
}
return str;
}
- 使用System.Convert.ToString将字符串转换成utf16进制编码
- utf16编码转换成base64编码(一)
- toString用法:将对象转换成字符串
- wchar_t 转换UTF16编码
- ToString()和Convert.ToString()字符串转换的区别
- 二者都是将 byte[] 转换成字符串,一个区别在于 Convert.ToBase64String 使用 base 64 数字编码,所以它生成的全部是 ASCII 字符。
- 使用C#,不使用任何Parse,TryParse,Convert方法,将数字字符串转换成整数
- Delphi REST 服务器返回UTF16编码转换成正常string
- Delphi REST 服务器返回UTF16编码转换成正常string
- 使用Convert.ToString
- C#中使用System.Convert类将一种基础数据类型转换为其他基础数据类型
- 网友面试题:C#,不使用任何Parse,TryParse,Convert方法,自写函数将数字字符串转换成整数
- java如何将字符转换成‘16进制格式’的字符串编码
- 将字符串转换成System.Drawing.Color类型
- 将字符串转换成System.Drawing.Color类型
- C#将字符串转换成System.Drawing.Color类型
- 将 System::String 转换为标准字符串
- 字符串转换toString()
- 微软免费企业邮局申请教程
- 如何在IIS上搭建WAP网站
- Google让你突破下载
- 取得表结构详细信息的SQL 语句
- ReportService的日期参数计算的一个技巧
- 使用System.Convert.ToString将字符串转换成utf16进制编码
- 通过url访问Reporting Service中的报表
- 轻松实现可伸缩性,容错性,和负载平衡的大规模多人在线系统
- Some joke
- JSP基础知识
- 用C99进行开放源代码的开发
- 个人感受
- ASP.NET程序中常用的三十三种代码
- [转]书非K不能读