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/
随机推荐
- 鼎信通达gsm网关和asterisk对接的调试
设备型号:4gsm卡,型号是DWG2000C-4Egsm网关IP地址: 10.3.6.250asteriskIP地址: 10.3.6.251 1)首先在asterisk里面添加到gsm网关的中继,并做 ...
- ftime() 系统时间
ftime() 函数,这个函数是取系统的时间,精确到毫秒级别,它在windows 和linux 下都可用.所以我暂时是比较喜欢它的. 这个函数返回一个结构体,结构体中两个成员,其中time 成员,与函 ...
- [ActionScript] AS3利用SWFObject与JS通信
首先介绍SWFObject的用法: swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flash ...
- Android Screen Monitor抓取真机屏幕
今天看到一款有点意思的开源软件“android-screen-monitor”, 简要记录如下: 1 简介 一款同步手机真机屏幕到PC上的软件(屏幕实时抓取,有点小卡) 2 开源地址 http://c ...
- OLAP如何实现数据的聚合计算-分析过程
- What is Split Brain in Oracle Clusterware and Real Application Cluster (文档 ID 1425586.1)
In this Document Purpose Scope Details 1. Clusterware layer 2. Real Application Cluster (d ...
- 菜鸟-手把手教你把Acegi应用到实际项目中(1.1)
相信不少朋友们对于学习Acegi的过程是比较痛苦的,而且可能最初一个例子都没能真正运行起来.即使能运行起来,对于里面那么多的配置,更搞不清楚为什么要那么配,多配一个和少配一个究竟有什么区别? 最终头都 ...
- 《Head First 设计模式》ch.3 装饰(Decorator)模式
设计原则 类应该对修改关闭,对扩展开放(开放-关闭原则).在每个地方使用开放-关闭原则是一种浪费,也没有必要,因为这通常会引入新的抽象层次,增加代码复杂度.需要把注意力集中在设计中最有可能改变的地方. ...
- Redis多机功能总结
1.通过Redis的复制功能,用户可以创建指定服务器的任意多个复制品,每个复制品服务器和被复制的原服务器拥有相同的数据: 2.通过将读请求分散给多个从服务器处理,用户可以减少主服务器在处理读请求方面的 ...
- cocos2d-lua class 方法解释
lua中没有类的概念,有的只是表(table),而类之间的继承也就是将父类的表连到了一起,派生类中没有找到的属性和方法就通过元表查找父类,在cocos2d-lua中,封装好的class方法,完美的实现 ...