辅助转换
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace HexTool
6
{
7
public class Common
8
{
9
/// <summary>
10
/// 16进制数据包转Byte[]
11
/// </summary>
12
/// <param name="str"></param>
13
/// <returns></returns>
14
public static byte[] HexDataPackStrToByte(string str)
15
{
16
string[] Strs = str.Split(' ');
17
byte[] DataBytes = new byte[Strs.Length];
18
for (int i = 0; i < Strs.Length; i++)
19
{
20
for (int j = Strs[i].Length - 1; j >= 0; j--)
21
{
22
int dataDlgorism = Convert.ToInt32(Strs[i][j]);
23
if (dataDlgorism > 9)
24
{
25
DataBytes[i] += (byte)(StrToDlgorism(Strs[i][j].ToString()) * (j == 0 ? 16 : 1));
26
}
27
else
28
{
29
DataBytes[i] += (byte)(dataDlgorism * (j == 0 ? 16 : 1));
30
}
31
}
32
}
33
34
return DataBytes;
35
}
36
37
/// <summary>
38
/// 字符串转Byte[]
39
/// </summary>
40
/// <param name="str"></param>
41
/// <returns></returns>
42
public static byte[] StrToBytes(string str)
43
{
44
byte[] data = Encoding.UTF8.GetBytes(str);
45
return data;
46
}
47
48
/// <summary>
49
/// Byte[]转AscII对照表字符串
50
/// </summary>
51
/// <param name="Bytes"></param>
52
/// <returns></returns>
53
public static string ByteToCharString(byte[] Bytes)
54
{
55
string chars = "";
56
for (int i = 0; i < Bytes.Length; i++)
57
{
58
char a = (char)Bytes[i];
59
if (a == '\0')
60
{
61
a = '.';
62
}
63
chars += a;
64
}
65
66
return chars;
67
}
68
69
/// <summary>
70
/// Byte[]转GB2312对照表字符串
71
/// </summary>
72
/// <param name="Bytes"></param>
73
/// <returns></returns>
74
public static string ByteToGB2312String(byte[] Bytes)
75
{
76
return Encoding.GetEncoding("GB2312").GetString(Bytes);
77
}
78
79
/// <summary>
80
/// Byte[]转Unicode对照表字符串
81
/// </summary>
82
/// <param name="Bytes"></param>
83
/// <returns></returns>
84
public static string ByteToUnicodeString(byte[] Bytes)
85
{
86
return Encoding.Unicode.GetString(Bytes);
87
}
88
89
/// <summary>
90
/// 字符串转GB2312字节数组
91
/// </summary>
92
/// <param name="str"></param>
93
/// <returns></returns>
94
public static byte[] StringToGB2312Bytes(string str)
95
{
96
return Encoding.GetEncoding("GB2312").GetBytes(str);
97
}
98
99
/// <summary>
100
/// 字符串转Unicode字节数组
101
/// </summary>
102
/// <param name="str"></param>
103
/// <returns></returns>
104
public static byte[] StringToUnicodeBytes(string str)
105
{
106
return Encoding.Unicode.GetBytes(str);
107
}
108
109
/// <summary>
110
/// Byte[]转16进制字符串
111
/// </summary>
112
/// <param name="Bytes"></param>
113
/// <returns></returns>
114
public static string BytesToHexString(byte[] Bytes)
115
{
116
string str = "";
117
for (int i = 0; i < Bytes.Length; i++)
118
{
119
string s = Bytes[i].ToString("X");
120
if (s.Length < 2)
121
{
122
s = "0" + s;
123
}
124
125
if (i < Bytes.Length - 1)
126
{
127
str += s + " ";
128
}
129
else
130
{
131
str += s;
132
}
133
}
134
135
return str;
136
}
137
138
/// <summary>
139
/// 字符串转10进制
140
/// </summary>
141
/// <returns></returns>
142
public static int StrToDlgorism(string str)
143
{
144
string[] HexStrs = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
145
string UpperStr = str.ToUpper();
146
147
for (int i = 0; i < HexStrs.Length; i++)
148
{
149
if (HexStrs[i] == UpperStr)
150
{
151
return i;
152
}
153
}
154
155
return 0;
156
157

212
}
213
214
/// <summary>
215
/// 把16进制字符串转换为比较方便看的字符串格式(每两位用空格隔开)
216
/// </summary>
217
/// <param name="?"></param>
218
/// <returns></returns>
219
public static string HexStrSplitDisStr(string str)
220
{
221
return SplitStr(str, 2, " ");
222
}
223
224
/// <summary>
225
/// 用字符号分割字符串
226
/// </summary>
227
/// <param name="str">字符串</param>
228
/// <param name="SplitLen">每多少个字符串分割</param>
229
/// <param name="SplitChar">分割符号</param>
230
/// <returns></returns>
231
public static string SplitStr(string str, int SplitLen, string SplitChar)
232
{
233
string newStr = "";
234
for (int i = 0; i < str.Length; i++)
235
{
236
newStr += str.Substring(i, 1);
237
if ((i + 1) % SplitLen == 0 && i < str.Length - 1)
238
{
239
newStr += SplitChar;
240
}
241
}
242
243
return newStr;
244
}
245
246
/// <summary>
247
/// MD5加密32位
248
/// </summary>
249
/// <param name="str"></param>
250
/// <returns></returns>
251
public static string Md5_32(string str)
252
{
253
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
254
byte[] md5Bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
255
string result = "";
256
for (int i = 0; i < md5Bytes.Length; i++)
257
{
258
result += md5Bytes[i].ToString("X").PadLeft(2, '0');
259
}
260
return result;
261
}
262
263
/// <summary>
264
/// MD5加密16位
265
/// </summary>
266
/// <param name="str"></param>
267
/// <returns></returns>
268
public static string Md5_16(string str)
269
{
270
return Md5_32(str).Substring(8, 16);
271
}
272
}
273
}
274
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace HexTool6
{7
public class Common8
{9
/// <summary>10
/// 16进制数据包转Byte[]11
/// </summary>12
/// <param name="str"></param>13
/// <returns></returns>14
public static byte[] HexDataPackStrToByte(string str)15
{16
string[] Strs = str.Split(' ');17
byte[] DataBytes = new byte[Strs.Length];18
for (int i = 0; i < Strs.Length; i++)19
{20
for (int j = Strs[i].Length - 1; j >= 0; j--)21
{22
int dataDlgorism = Convert.ToInt32(Strs[i][j]);23
if (dataDlgorism > 9)24
{25
DataBytes[i] += (byte)(StrToDlgorism(Strs[i][j].ToString()) * (j == 0 ? 16 : 1));26
}27
else28
{29
DataBytes[i] += (byte)(dataDlgorism * (j == 0 ? 16 : 1));30
}31
}32
}33

34
return DataBytes;35
}36

37
/// <summary>38
/// 字符串转Byte[]39
/// </summary>40
/// <param name="str"></param>41
/// <returns></returns>42
public static byte[] StrToBytes(string str)43
{44
byte[] data = Encoding.UTF8.GetBytes(str);45
return data;46
}47

48
/// <summary>49
/// Byte[]转AscII对照表字符串50
/// </summary>51
/// <param name="Bytes"></param>52
/// <returns></returns>53
public static string ByteToCharString(byte[] Bytes)54
{55
string chars = "";56
for (int i = 0; i < Bytes.Length; i++)57
{58
char a = (char)Bytes[i];59
if (a == '\0')60
{61
a = '.';62
}63
chars += a;64
}65

66
return chars;67
}68

69
/// <summary>70
/// Byte[]转GB2312对照表字符串71
/// </summary>72
/// <param name="Bytes"></param>73
/// <returns></returns>74
public static string ByteToGB2312String(byte[] Bytes)75
{76
return Encoding.GetEncoding("GB2312").GetString(Bytes);77
}78

79
/// <summary>80
/// Byte[]转Unicode对照表字符串81
/// </summary>82
/// <param name="Bytes"></param>83
/// <returns></returns>84
public static string ByteToUnicodeString(byte[] Bytes)85
{86
return Encoding.Unicode.GetString(Bytes);87
}88

89
/// <summary>90
/// 字符串转GB2312字节数组91
/// </summary>92
/// <param name="str"></param>93
/// <returns></returns>94
public static byte[] StringToGB2312Bytes(string str)95
{96
return Encoding.GetEncoding("GB2312").GetBytes(str);97
}98

99
/// <summary>100
/// 字符串转Unicode字节数组101
/// </summary>102
/// <param name="str"></param>103
/// <returns></returns>104
public static byte[] StringToUnicodeBytes(string str)105
{106
return Encoding.Unicode.GetBytes(str);107
}108

109
/// <summary>110
/// Byte[]转16进制字符串111
/// </summary>112
/// <param name="Bytes"></param>113
/// <returns></returns>114
public static string BytesToHexString(byte[] Bytes)115
{116
string str = "";117
for (int i = 0; i < Bytes.Length; i++)118
{119
string s = Bytes[i].ToString("X");120
if (s.Length < 2)121
{122
s = "0" + s;123
}124

125
if (i < Bytes.Length - 1)126
{127
str += s + " ";128
}129
else130
{131
str += s;132
}133
}134

135
return str;136
}137

138
/// <summary>139
/// 字符串转10进制140
/// </summary>141
/// <returns></returns>142
public static int StrToDlgorism(string str)143
{144
string[] HexStrs = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };145
string UpperStr = str.ToUpper();146

147
for (int i = 0; i < HexStrs.Length; i++)148
{149
if (HexStrs[i] == UpperStr)150
{151
return i;152
}153
}154

155
return 0;156

157

212
}213

214
/// <summary>215
/// 把16进制字符串转换为比较方便看的字符串格式(每两位用空格隔开)216
/// </summary>217
/// <param name="?"></param>218
/// <returns></returns>219
public static string HexStrSplitDisStr(string str)220
{221
return SplitStr(str, 2, " ");222
}223

224
/// <summary>225
/// 用字符号分割字符串226
/// </summary>227
/// <param name="str">字符串</param>228
/// <param name="SplitLen">每多少个字符串分割</param>229
/// <param name="SplitChar">分割符号</param>230
/// <returns></returns>231
public static string SplitStr(string str, int SplitLen, string SplitChar)232
{233
string newStr = "";234
for (int i = 0; i < str.Length; i++)235
{236
newStr += str.Substring(i, 1);237
if ((i + 1) % SplitLen == 0 && i < str.Length - 1)238
{239
newStr += SplitChar;240
}241
}242

243
return newStr;244
}245

246
/// <summary>247
/// MD5加密32位248
/// </summary>249
/// <param name="str"></param>250
/// <returns></returns>251
public static string Md5_32(string str)252
{253
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();254
byte[] md5Bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(str));255
string result = "";256
for (int i = 0; i < md5Bytes.Length; i++)257
{258
result += md5Bytes[i].ToString("X").PadLeft(2, '0');259
}260
return result;261
}262

263
/// <summary>264
/// MD5加密16位265
/// </summary>266
/// <param name="str"></param>267
/// <returns></returns>268
public static string Md5_16(string str)269
{270
return Md5_32(str).Substring(8, 16);271
}272
}273
}274


浙公网安备 33010602011771号