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进制编码(转)的更多相关文章

  1. 数组中hashCode就是内存地址,以及汉字幻化为16进制或10进制

    int[] arr4={1,2,3,4,5}; System.out.println("arr4: "+arr4); System.out.println("arr4.h ...

  2. java中将汉字转换成16进制

    技术交流群:233513714 /** * 将汉字转换车16进制字符串 * @param str * @return st */ public static String enUnicode(Stri ...

  3. Java 将字节数组转化为16进制的多种方案

    很多时候我们需要将字节数组转化为16进制字符串来保存,尤其在很多加密的场景中,例如保存密钥等.因为字节数组,除了写入文件或者以二进制的形式写入数据库以外,无法直接转为为字符串,因为字符串结尾有\0,当 ...

  4. .NET Core RSA 签名和验签(密钥为 16 进制编码)

    使用 OpenSSL 生成公私钥对,命令: $ openssl genrsa -out rsa_1024_priv.pem $ openssl pkcs8 -topk8 -inform PEM -in ...

  5. iOS开发时间戳与时间NSDate,时区的转换,汉字与UTF8,16进制的转换

    http://blog.sina.com.cn/s/blog_68661bd80101njdo.html 标签: ios时间戳 ios开发时间戳 ios16进制转中文 ios开发utf8转中文 ios ...

  6. C#把汉字转换成16进制(HEX)并向串口发送数据

    报警器实例:(有发送,无返回获取) using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  7. 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 ...

  8. JAVA byte数组转化为16进制字符串输出

    最简单的方法: 利用javax.xml.bind包下的DatatypeConverter printHexBinary public static java.lang.String printHexB ...

  9. rgba转化为16进制在线工具

    https://www.sioe.cn/yingyong/yanse-rgb-16/

随机推荐

  1. ERP_Oracle Fusion Application新一代ERP介绍

    2014-12-31 Created By BaoXinjian

  2. OAF_EO系列5 - Update详解和实现(案例)

    2014-06-14 Created By BaoXinjian

  3. 在Where中对列使用函数,将导致其不可索引

    在Sql语句的Select部分对字段编写标量函数是完全可以的,但是下面代码: select EmpNo,LastName from Emp 应当写为 select EmpNo,LastName fro ...

  4. (转)zookeeper学习记录--附browser

    转自:http://agapple.iteye.com/blog/1111377 背景 前段时间看了S4流计算引擎,里面使用到了zookeeper进行集群管理,所以也就花了点时间研究了下zookeep ...

  5. Log4j乱码

    在log4j.properties里添加encoding , 例如 : log4j.appender.fuscent=org.apache.log4j.DailyRollingFileAppender ...

  6. 树莓派+qt+opencv

    树莓派:Qt开发套件和opencv安装sudo apt-get install qt4-dev-tools libqt4-dev 不用qt core 的话,其实只要按照 qt4-qmake就行了(只做 ...

  7. python第一个hello world注意问题!!

    如果你第一次写python代码,想写一个通常的hello world ,那么你需要注意这个hello world的写法,这和python的版本有直接关系!!! Python 3.x: print('h ...

  8. InnoSetup打包exe安装应用程序,并添加卸载图标 转

    http://blog.csdn.net/guoquanyou/article/details/7445773 InnoSetup真是一个非常棒的工具.给我的印象就是非常的精干.所以,该工具已经一步步 ...

  9. Android 上的代码阅读器 CoderBrowserHD 修改支持 go 语言代码

    我在Android上的代码阅读器用的是 https://github.com/zerob13/CoderBrowserHD 改造的版本,改造后的版本我放在 https://github.com/ghj ...

  10. python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求

    一.JSON简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programm ...