1 1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串
2
3 //十进制转二进制
4 Console.WriteLine("十进制166的二进制表示: "+Convert.ToString(166, 2));
5 //十进制转八进制
6 Console.WriteLine("十进制166的八进制表示: "+Convert.ToString(166, 8));
7 //十进制转十六进制
8 Console.WriteLine("十进制166的十六进制表示: "+Convert.ToString(166, 16));
9
10 //二进制转十进制
11 Console.WriteLine("二进制 111101 的十进制表示: "+Convert.ToInt32("111101", 2));
12 //八进制转十进制
13 Console.WriteLine("八进制 44 的十进制表示: "+Convert.ToInt32("44", 8));
14 //十六进制转十进制
15 Console.WriteLine("十六进制 CC的十进制表示: "+Convert.ToInt32("CC", 16));
16
17 2.在串口通讯过程中,经常要用到 16进制与字符串、字节数组之间的转换
18 //
19
20 private string StringToHexString(string s,Encoding encode)
21 {
22 byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
23 string result = string.Empty;
24 for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符,以%隔开
25 {
26 result += "%"+Convert.ToString(b[i], 16);
27 }
28 return result;
29 }
30 private string HexStringToString(string hs, Encoding encode)
31 {
32 //以%分割字符串,并去掉空字符
33 string[] chars = hs.Split(new char[]{'%'},StringSplitOptions.RemoveEmptyEntries);
34 byte[] b = new byte[chars.Length];
35 //逐个字符变为16进制字节数据
36 for (int i = 0; i < chars.Length; i++)
37 {
38 b[i] = Convert.ToByte(chars[i], 16);
39 }
40 //按照指定编码将字节数组变为字符串
41 return encode.GetString(b);
42 }
43
44
45
46
47 /// <summary>
48 /// 字符串转16进制字节数组
49 /// </summary>
50 /// <param name="hexString"></param>
51 /// <returns></returns>
52 private static byte[] strToToHexByte(string hexString)
53 {
54 hexString = hexString.Replace(" ", "");
55 if ((hexString.Length % 2) != 0)
56 hexString += " ";
57 byte[] returnBytes = new byte[hexString.Length / 2];
58 for (int i = 0; i < returnBytes.Length; i++)
59 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
60 return returnBytes;
61 }
62
63
64
65
66
67 /// <summary>
68 /// 字节数组转16进制字符串
69 /// </summary>
70 /// <param name="bytes"></param>
71 /// <returns></returns>
72 public static string byteToHexStr(byte[] bytes)
73 {
74 string returnStr = "";
75 if (bytes != null)
76 {
77 for (int i = 0; i < bytes.Length; i++)
78 {
79 returnStr += bytes[i].ToString("X2");
80 }
81 }
82 return returnStr;
83 }
84
85
86
87
88
89 /// <summary>
90 /// 从汉字转换到16进制
91 /// </summary>
92 /// <param name="s"></param>
93 /// <param name="charset">编码,如"utf-8","gb2312"</param>
94 /// <param name="fenge">是否每字符用逗号分隔</param>
95 /// <returns></returns>
96 public static string ToHex(string s, string charset, bool fenge)
97 {
98 if ((s.Length % 2) != 0)
99 {
100 s += " ";//空格
101 //throw new ArgumentException("s is not valid chinese string!");
102 }
103 System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
104 byte[] bytes = chs.GetBytes(s);
105 string str = "";
106 for (int i = 0; i < bytes.Length; i++)
107 {
108 str += string.Format("{0:X}", bytes[i]);
109 if (fenge && (i != bytes.Length - 1))
110 {
111 str += string.Format("{0}", ",");
112 }
113 }
114 return str.ToLower();
115 }
116
117
118
119
120
121 ///<summary>
122 /// 从16进制转换成汉字
123 /// </summary>
124 /// <param name="hex"></param>
125 /// <param name="charset">编码,如"utf-8","gb2312"</param>
126 /// <returns></returns>
127 public static string UnHex(string hex, string charset)
128 {
129 if (hex == null)
130 throw new ArgumentNullException("hex");
131 hex = hex.Replace(",", "");
132 hex = hex.Replace("\n", "");
133 hex = hex.Replace("\\", "");
134 hex = hex.Replace(" ", "");
135 if (hex.Length % 2 != 0)
136 {
137 hex += "20";//空格
138 }
139 // 需要将 hex 转换成 byte 数组。
140 byte[] bytes = new byte[hex.Length / 2];
141
142 for (int i = 0; i < bytes.Length; i++)
143 {
144 try
145 {
146 // 每两个字符是一个 byte。
147 bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
148 System.Globalization.NumberStyles.HexNumber);
149 }
150 catch
151 {
152 // Rethrow an exception with custom message.
153 throw new ArgumentException("hex is not a valid hex number!", "hex");
154 }
155 }
156 System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
157 return chs.GetString(bytes);
158 }
来源:oschina
链接:https://my.oschina.net/u/4388097/blog/4239334