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. BIP_开发案例08_BI Publisher图表示例 饼状图/直方图/折线图(案例)

    2014-12-25 Created By BaoXinjian

  2. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  3. Centos下使用gitosis配置管理git服务端(转载)

    From:http://www.cnblogs.com/ahauzyy/archive/2013/04/08/3043384.html 说明:由于条件有限,我这里使用的是同一台centos的,但教程内 ...

  4. Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面 z

    http://www.cnblogs.com/zuowj/p/4504130.html 不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景 也最为 ...

  5. SQL查询包含汉字的行

    1.查询字段首位为汉字 2.查询字段包含汉字(任意位) SELECT * FROM 表名 WHERE 字段 LIKE '%[吖-座]%' --[吖-座]是中文字符集第一个到最后一个的范围

  6. [HDU 4821] String (字符串哈希)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个? 哈希+枚 ...

  7. Machine Schedule(最小覆盖)

    其实也是个最小覆盖问题 关于最小覆盖http://blog.csdn.net/u014665013/article/details/49870029 Description As we all kno ...

  8. 11gR2 Clusterware and Grid Home - What You Need to Know

    11gR2 Clusterware Key Facts 11gR2 Clusterware is required to be up and running prior to installing a ...

  9. jdk集合常用方法分析之ArrayList&LinkedList&以及两者的对比分析

    集合使用注意事项: 1.集合当中只能放置对象的引用,无法放置原生数据类型,我们需要使用原生数据类型的包装类才能加入到集合当中去(JDK5之后会进行自动的装箱和拆箱操作,表面上看集合中是可以直接放置原生 ...

  10. HIVE配置文件

    进入HIVE_HOME/conf 编辑文件hive-site.xml,内容如下:(这是伪分布式模式) 主要声明了以下几个内容: 数据仓库地址 数据库连接地址 数据库连接驱动 数据库连接用户名 数据库连 ...