字符串/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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM