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/
随机推荐
- OSTimeDelay(1)
当OS_TICKS_PER_SEC=100HZ ,即每个时钟节拍10ms;如果想延时10ms而调用OSTimeDelay(1)是不行的,调用该函数至少要2个时钟节拍才不会因时间太短,调度跟不上而产生错 ...
- JAVA赋值运算符
赋值预算符,简单来说就是把以及定义了值的变量值赋值给刚定义的变量 例如,a学员,与b学员的成绩相同,a学员是80分,我们根据a学员的成绩,输出b学员的成绩. public class Test{ pu ...
- sql server异地备份数据库
--注意:异地备份数据库,需要先开启备份文件夹的网络共享,数据库选择master --打开允许执行xp_cmdshell exec master..xp_cmdshell 'net use \\192 ...
- python 读取文本
将文本转换到NumPy 数组中,做机器学习或其他任何任务,文本处理的技能必不可少.python 实现实现了很精简强大的文本处理功能: 假设 文件 traindata.csv 中有数据 1000行,3列 ...
- 脱离rails 使用Active Record
目录结构 database.yml development: adapter: sqlite3 database: db/test.db pool: 5 timeout: 5000 001_schem ...
- NPOI高效匯出Excel
using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using NPOI. ...
- 用SPCOMM 在 Delphi中实现串口通讯 转
用Delphi 实现串口通讯,常用的几种方法为:使用控件如MSCOMM和SPCOMM,使用API函数或者在Delphi 中调用其它串口通讯程序.利用API编写串口通信程序较为复杂,需要掌握大量通信 ...
- MySQL 字符串 转 int/double CAST与CONVERT 函数的用法
http://hongmin118.iteye.com/blog/2029728 转的 MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值.两者具体的语 ...
- Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean ...
- 使用Semaphore控制并发访问
Semaphore,信号量. 用在多线程环境下对共享资源访问的一种协调机制. 当一个线程想要访问共享的资源时,这个线程需要获取Semaphore,如果Semaphore内部计数器的值大于0,Semap ...