16进制与字符串、字节数组之间的转换(一)
11.请问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
172.在串口通讯过程中,经常要用到 16进制与字符串、字节数组之间的转换
18//
19
20privatestring StringToHexString(string s,Encoding encode)
21 {
22byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
23string result = string.Empty;
24for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符,以%隔开
25 {
26 result += "%"+Convert.ToString(b[i], 16);
27 }
28return result;
29 }
30privatestring HexStringToString(string hs, Encoding encode)
31 {
32//以%分割字符串,并去掉空字符
33string[] chars = hs.Split(newchar[]{'%'},StringSplitOptions.RemoveEmptyEntries);
34byte[] b = newbyte[chars.Length];
35//逐个字符变为16进制字节数据
36for (int i = 0; i < chars.Length; i++)
37 {
38 b[i] = Convert.ToByte(chars[i], 16);
39 }
40//按照指定编码将字节数组变为字符串
41return encode.GetString(b);
42 }
43
44
45
46
47///<summary>
48/// 字符串转16进制字节数组
49///</summary>
50///<param name="hexString"></param>
51///<returns></returns>
52privatestaticbyte[] strToToHexByte(string hexString)
53 {
54 hexString = hexString.Replace("", "");
55if ((hexString.Length % 2) != 0)
56 hexString += "";
57byte[] returnBytes = newbyte[hexString.Length / 2];
58for (int i = 0; i < returnBytes.Length; i++)
59 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
60return returnBytes;
61 }
62
63
64
65
66
67///<summary>
68/// 字节数组转16进制字符串
69///</summary>
70///<param name="bytes"></param>
71///<returns></returns>
72publicstaticstring byteToHexStr(byte[] bytes)
73 {
74string returnStr = "";
75if (bytes != null)
76 {
77for (int i = 0; i < bytes.Length; i++)
78 {
79 returnStr += bytes[i].ToString("X2");
80 }
81 }
82return 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>
96publicstaticstring ToHex(string s, string charset, bool fenge)
97 {
98if ((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);
104byte[] bytes = chs.GetBytes(s);
105string str = "";
106for (int i = 0; i < bytes.Length; i++)
107 {
108 str += string.Format("{0:X}", bytes[i]);
109if (fenge && (i != bytes.Length - 1))
110 {
111 str += string.Format("{0}", ",");
112 }
113 }
114return 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>
127publicstaticstring UnHex(string hex, string charset)
128 {
129if (hex == null)
130thrownew ArgumentNullException("hex");
131 hex = hex.Replace(",", "");
132 hex = hex.Replace("\n", "");
133 hex = hex.Replace("\\", "");
134 hex = hex.Replace("", "");
135if (hex.Length % 2 != 0)
136 {
137 hex += "20";//空格
138 }
139// 需要将 hex 转换成 byte 数组。
140byte[] bytes = newbyte[hex.Length / 2];
141
142for (int i = 0; i < bytes.Length; i++)
143 {
144try
145 {
146// 每两个字符是一个 byte。
147 bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
148 System.Globalization.NumberStyles.HexNumber);
149 }
150catch
151 {
152// Rethrow an exception with custom message.
153thrownew ArgumentException("hex is not a valid hex number!", "hex");
154 }
155 }
156 System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
157return chs.GetString(bytes);
158 }
以上是 16进制与字符串、字节数组之间的转换(一) 的全部内容, 来源链接: utcz.com/z/510177.html