c# 字符串(含有汉字)转化为16进制编码(转)
public static string Str2Hex(string s)
{
string result = string.Empty;
byte[] arrByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s);
for(int i = 0; i < arrByte.Length; i++)
{
result += "&#x" + System.Convert.ToString(arrByte[i], 16) + ";"; //Convert.ToString(byte, 16)把byte转化成十六进制string
}
return result;
}
变成可以在网上传输的那种16进制编码,类似%8D%E2这种?这样的话,
用System.Web.HTTPUtility.URLEncode()就行。
光光10进制转换到16进制的话,可以用 bytes(i).ToString("X"),
这是将一个字节转换为一个16进制字符串,"X"表示大写16进制字符,用"x"可以得到小写的。
参考
字符串(含有汉字)转化为ascII16进制问题
http://topic.csdn.net/t/20040905/22/3342635.html
加码解码
http://xiaodi.cnblogs.com/archive/2005/04/26/145493.aspx
public string EncodingSMS(string s)
{
string result = string.Empty;
byte[] arrByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(s);
for(int i = 0; i < arrByte.Length; i++)
{
result += System.Convert.ToString(arrByte[i], 16); //Convert.ToString(byte, 16)把byte转化成十六进制string
}
return result;
}
public string DecodingSMS(string s)
{
string result = string.Empty;
byte[] arrByte = new byte[s.Length / 2];
int index = 0;
for(int i = 0; i < s.Length; i += 2)
{
arrByte[index++] = Convert.ToByte(s.Substring(i,2),16); //Convert.ToByte(string,16)把十六进制string转化成byte
}
result = System.Text.Encoding.Default.GetString(arrByte);
return result;
}
加码解码的规则如下:
加码时将字符串中的所有字符转换成其对应的ASCII值的16进制值,例如:“A”的ASCII码值为65,以16进制值表示为41,故应发送两个字符“41”以代表字符“A”。
对于汉字则以其内码的16进制值来表示,如“测试”应为:B2E2CAD4。
原理:
string aaa = "AB测试";
byte[] bbb = System.Text.Encoding.Default.GetBytes(aaa);
string ccc = System.Text.Encoding.Default.GetString(bbb);
for(int i = 0; i < bbb.Length; i++)
{
Response.Write(System.Convert.ToString(bbb[i], 16));
}
Response.Write(ccc);
c# 字符串(含有汉字)转化为16进制编码(转)的更多相关文章
- 数组中hashCode就是内存地址,以及汉字幻化为16进制或10进制
int[] arr4={1,2,3,4,5}; System.out.println("arr4: "+arr4); System.out.println("arr4.h ...
- java中将汉字转换成16进制
技术交流群:233513714 /** * 将汉字转换车16进制字符串 * @param str * @return st */ public static String enUnicode(Stri ...
- Java 将字节数组转化为16进制的多种方案
很多时候我们需要将字节数组转化为16进制字符串来保存,尤其在很多加密的场景中,例如保存密钥等.因为字节数组,除了写入文件或者以二进制的形式写入数据库以外,无法直接转为为字符串,因为字符串结尾有\0,当 ...
- .NET Core RSA 签名和验签(密钥为 16 进制编码)
使用 OpenSSL 生成公私钥对,命令: $ openssl genrsa -out rsa_1024_priv.pem $ openssl pkcs8 -topk8 -inform PEM -in ...
- iOS开发时间戳与时间NSDate,时区的转换,汉字与UTF8,16进制的转换
http://blog.sina.com.cn/s/blog_68661bd80101njdo.html 标签: ios时间戳 ios开发时间戳 ios16进制转中文 ios开发utf8转中文 ios ...
- C#把汉字转换成16进制(HEX)并向串口发送数据
报警器实例:(有发送,无返回获取) using System; using System.Collections.Generic; using System.Linq; using System.Te ...
- LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- JAVA byte数组转化为16进制字符串输出
最简单的方法: 利用javax.xml.bind包下的DatatypeConverter printHexBinary public static java.lang.String printHexB ...
- rgba转化为16进制在线工具
https://www.sioe.cn/yingyong/yanse-rgb-16/
随机推荐
- Java 查看死锁的方法
那我们怎么确定一定是死锁呢?有两种方法. 1>使用JDK给我们的的工具JConsole,可以通过打开cmd然后输入jconsole打开. 1)连接到需要查看的进程.
- 一款监控网络状态的好工具- Smokeping
最近工作中需要监测某个分公司到IDC机房的网络情况,到网络上找了不少软件,发现一款叫smokeping的开源软件还不错,它是rrdtool的作者制作的,在图形显示方面很漂亮,可以用来很好的检测网络状态 ...
- Gradle学习
Gradle是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是采用一种基于Groovy的内部领域特定语言.近期,Gradle获得了极大的关注,这也是我决定去研究Gradle的原因. 这篇文章是 ...
- nfs不能自动mount(转载)
From:http://www.wenzizone.com/2009/08/14/nfs_can_not_automount_supplementary.html 手动挂载nfs没有问题,说明port ...
- Yii 2.0 单文件上传
先创建一个(UploadForm.php)模型层 <?phpnamespace app\models; use yii\base\Model;use yii\web\UploadedFile; ...
- .NET 验证码/验证图片
/// <summary> /// 验证码类 /// </summary> public class Rand { #region 生成随机数字 /// <summary ...
- GridView内容<br />换行
if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[].Text = Server.HtmlDecode(e.Row.Cell ...
- 微信红包签名算法 C#代码实现
string stringA = "appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=100001 ...
- linux 多网卡 跃点数
centos6.4 配置两块网卡,eth0设置静态IP,8网段,eth1无线配置dhcp,都是开机启动. 但是eth1无线网卡一旦连接至开放网络(需要web登陆),就替换了之前eth0配置的默认网关, ...
- null 之AddAll、Add和Boolean
@Test //failed public void TestListAddAll(){ List<TravelerInfo> travelerInfoSummary=new ArrayL ...