其他分享
首页 > 其他分享> > 字符串/16进制/ASCII码的转换

字符串/16进制/ASCII码的转换

作者:互联网

 1         /// <字符串转16进制格式,不够自动前面补零>
 2         /// 假设文本框里面填写的是:01 02 03 04 05 06
 3         /// Str获取的是01 02 03 04 05 06 是字符串
 4         /// 需要将01转为0x01,02转为0x02
 5         /// 首先默认把文本框每隔两个字符作为一个16进制数
 6         /// 假设文本框中是010203040506(偶数个数据),则01,02,03,04,05,06分别作为16进制
 7         /// 如果文本框中的字符个数是奇数个1115268,则默认先把前面每隔两个作为一个16进制数,最后一个数前面补零0x11,0x15,0x26,0x08
 8         /// </summary>
 9         /// <param name="hexString"></param>
10         /// <returns>返回字符数组</returns>
11         public static byte[] strToToHexByte(String hexString) {
12             int i;
13             hexString = hexString.Replace(" ", "");//清除空格
14             if ((hexString.Length % 2) != 0)//奇数个
15             {
16                 byte[] returnBytes = new byte[(hexString.Length + 1) / 2];
17                 try {
18                     for (i = 0; i < (hexString.Length - 1) / 2; i++) {
19                         returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
20                     }
21                     returnBytes[returnBytes.Length - 1] = Convert.ToByte(hexString.Substring(hexString.Length - 1, 1).PadLeft(2, '0'), 16);
22                 } catch {
23                     MessageBox.Show("含有非16进制字符", "提示");
24                     return null;
25                 }
26                 return returnBytes;
27             } else {
28                 byte[] returnBytes = new byte[(hexString.Length) / 2];
29                 try {
30                     for (i = 0; i < returnBytes.Length; i++) {
31                         returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
32                     }
33                 } catch {
34                     MessageBox.Show("含有非16进制字符", "提示");
35                     return null;
36                 }
37                 return returnBytes;
38             }
39         }
40         /// <summary>
41         /// 将字符串s通过ASCII码转换为16进制字符串
42         /// </summary>
43         /// <param name="s"></param>
44         /// <param name="encode"></param>
45         /// <returns>十六进制字符串</returns>
46         private static string result;//string转16进制的结果
47         public static string stringToHexString(string s, Encoding encode) {
48             byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
49             result = string.Empty;
50             for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
51             {
52                 result += Convert.ToString(b[i], 16);
53             }
54             return result;
55         }
56 
57         /// <summary>
58         /// 将一条十六进制字符串转换为ASCII
59         /// </summary>
60         /// <param name="hexstring">一条十六进制字符串</param>
61         /// <returns>返回一条ASCII码</returns>
62         public static string hexStringToASCII(string hexstring) {
63             byte[] bt = hexStringToBinary(hexstring);
64             string lin = "";
65             for (int i = 0; i < bt.Length; i++) {
66                 lin = lin + bt[i] + " ";
67             }
68             string[] ss = lin.Trim().Split(new char[] { ' ' });
69             char[] c = new char[ss.Length];
70             int a;
71             for (int i = 0; i < c.Length; i++) {
72                 a = Convert.ToInt32(ss[i]);
73                 c[i] = Convert.ToChar(a);
74             }
75             string b = new string(c);
76             return b;
77         }
78         /// <summary>
79         /// 16进制字符串转换为二进制数组
80         /// </summary>
81         /// <param name="hexstring">用空格切割字符串</param>
82         /// <returns>返回一个二进制字符串</returns>
83         public static byte[] hexStringToBinary(string hexstring) { 
84             string[] tmpary = hexstring.Trim().Split(' ');
85             byte[] buff = new byte[tmpary.Length];
86             for (int i = 0; i < buff.Length; i++) {
87                 buff[i] = Convert.ToByte(tmpary[i], 16);
88             }
89             return buff;
90         }

 

标签:进制,16,hexString,Length,returnBytes,byte,ASCII,string
来源: https://www.cnblogs.com/Thesunkomorebi/p/15930223.html