字符串、ASCII码和16进制的转换方法

 1  /// <字符串转16进制格式,不够自动前面补零>
 2         /// 
 3         /// </summary>
 4         /// <param name="hexString"></param>
 5         /// <returns></returns>
 6         public static byte[] strToToHexByte(String hexString) {
 7             int i;
 8             hexString = hexString.Replace(" ", "");//清除空格
 9             if ((hexString.Length % 2) != 0)//奇数个
10             {
11                 byte[] returnBytes = new byte[(hexString.Length + 1) / 2];
12                 try {
13                     for (i = 0; i < (hexString.Length - 1) / 2; i++) {
14                         returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
15                     }
16                     returnBytes[returnBytes.Length - 1] = Convert.ToByte(hexString.Substring(hexString.Length - 1, 1).PadLeft(2, '0'), 16);
17                 } catch {
18                     MessageBox.Show("含有非16进制字符", "提示");
19                     return null;
20                 }
21                 return returnBytes;
22             } else {
23                 byte[] returnBytes = new byte[(hexString.Length) / 2];
24                 try {
25                     for (i = 0; i < returnBytes.Length; i++) {
26                         returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
27                     }
28                 } catch {
29                     MessageBox.Show("含有非16进制字符", "提示");
30                     return null;
31                 }
32                 return returnBytes;
33             }
34         }
35 
36         //string转16进制
37         public static string StringToHexString(string s, Encoding encode) {
38             byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
39             result = string.Empty;
40             for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
41             {
42                 result += Convert.ToString(b[i], 16);
43             }
44             return result;
45         }
46 
47         /// <summary>
48         /// 将一条十六进制字符串转换为ASCII
49         /// </summary>
50         /// <param name="hexstring">一条十六进制字符串</param>
51         /// <returns>返回一条ASCII码</returns>
52         public static string HexStringToASCII(string hexstring) {
53             byte[] bt = HexStringToBinary(hexstring);
54             string lin = "";
55             for (int i = 0; i < bt.Length; i++) {
56                 lin = lin + bt[i] + " ";
57             }
58 
59 
60             string[] ss = lin.Trim().Split(new char[] { ' ' });
61             char[] c = new char[ss.Length];
62             int a;
63             for (int i = 0; i < c.Length; i++) {
64                 a = Convert.ToInt32(ss[i]);
65                 c[i] = Convert.ToChar(a);
66             }
67 
68             string b = new string(c);
69             return b;
70         }
71         /// <summary>
72         /// 16进制字符串转换为二进制数组
73         /// </summary>
74         /// <param name="hexstring">用空格切割字符串</param>
75         /// <returns>返回一个二进制字符串</returns>
76         public static byte[] HexStringToBinary(string hexstring) {
77 
78             string[] tmpary = hexstring.Trim().Split(' ');
79             byte[] buff = new byte[tmpary.Length];
80             for (int i = 0; i < buff.Length; i++) {
81                 buff[i] = Convert.ToByte(tmpary[i], 16);
82             }
83             return buff;
84         }
上一篇:Go语言学习16-作用域与内存分析、递归函数


下一篇:Ajax获取 Json文件提取数据