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. DBA_Oracle Archive Log的基本应用和启用(概念)

    2014-11-15 Created By BaoXinjian

  2. Oracle数据库 External component has thrown an exception

    出现这种错误一般是在SQL语句执行前就报出的错误.这样的错误一般需要仔细检查SQL语句,以及参数的数据类型. 而在cmd.ExecuteNonQuery()中出现的错误,则很可能就排除了语法类的错误. ...

  3. [实变函数]5.2 非负简单函数的 Lebesgue 积分

    1 设        $$\bex        \phi(x)=\sum_{i=1}^j c_i\chi_{E_i}(x),\quad c_i\geq 0,        \eex$$ 其中     ...

  4. C#3

    定义常量:1.静态常量 const(定义时必须初始化)  ...常量都不可以改变 2.动态常量 readonly(定义时不必初始化) \\要在Main方法前面 用法:const double PI = ...

  5. 关于Switch结构利用

    三大流程结构,循环.分支.if ,循环与条件选择结构用的比较多,而swicth用的比较少,swicth可以用if代替,只不过麻烦,最终都能实现输入和输出的条件对应     Swicth利用       ...

  6. python函数参数前面单星号(*)和双星号(**)的区别

    在python的函数中经常能看到输入的参数前面有一个或者两个星号:例如 def foo(param1, *param2): def bar(param1, **param2): 这两种用法其实都是用来 ...

  7. IntelliJ IDEA 注册码

    IntelliJ IDEA 注册码 *.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀 请搭建自己的IntelliJ IDEA授权服务器,教程在http ...

  8. Linux自启动

    linux 下tomcat开机自启动修改Tomcat/bin/startup.sh 为:export JAVA_HOME=/usr/java/j2sdk1.4.2_08export CLASSPATH ...

  9. ramBufferSizeMB

    索引算法确定 的情况下,影响Lucene索引速度的因素 MaxBufferedDocs这个参数默认是disabled的,因为Lucene中还用另外一个参数(RAMBufferSizeMB)控制这个bu ...

  10. AngularJs在单击提交后显示验证信息.

    <!DOCTYPE html> <html ng-app="app"> <head> <meta charset="utf-8& ...