ConverHelper.cs
5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Text;
namespace HHECS.Infrastructure.CommonHelper
{
/// <summary>
/// 数据类型转换帮助类
/// </summary>
public static class ConverHelper
{
#region 字符串和Byte之间的转化
/// <summary>
/// 将字符串转为16进制字符,允许中文
/// </summary>
/// <param name="s"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string StringToHexString(string s, Encoding encode, string spanString)
{
byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
string result = string.Empty;
for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
{
result += Convert.ToString(b[i], 16) + spanString;
}
return result;
}
/// <summary>
/// 将16进制字符串转为字符串
/// </summary>
/// <param name="hs"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string HexStringToString(string hs, Encoding encode)
{
string strTemp = "";
byte[] b = new byte[hs.Length / 2];
for (int i = 0; i < hs.Length / 2; i++)
{
strTemp = hs.Substring(i * 2, 2);
b[i] = Convert.ToByte(strTemp, 16);
}
//按照指定编码将字节数组变为字符串
return encode.GetString(b);
}
/// <summary>
/// byte[]转为16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string ByteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
/// <summary>
/// 将16进制的字符串转为byte[]
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static byte[] StrToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
/// <summary>
/// ASCII码转字符
/// </summary>
/// <param name="ints"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static string ASCIIToString(short[] ints)
{
String str = "";
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
foreach (var item in ints)
{
if (item >= 0 && item <= 255)
{
byte[] byteArray = new byte[] { (byte)item };
string strCharacter = asciiEncoding.GetString(byteArray);
str += strCharacter;
}
else
{
throw new ArgumentException("ASCII Code is not valid.");
}
}
return str;
}
/// <summary>
/// 字符转ASCII,这里默认20位,不够填充空格
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static byte[] StringToASCII(string str)
{
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
return asciiEncoding.GetBytes(str.PadRight(20, ' '));
}
public static short Asc(string character)
{
if (character.Length == 1)
{
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
short intAsciiCode = (short)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
#endregion
#region 字节位操作
/// <summary>
/// 对于一个字节b,从0开始,获取左起第index位,比如第0位,第2位
/// </summary>
/// <param name="b"></param>
/// <param name="index"></param>
/// <returns></returns>
public static int GetBit(byte b, int index) { return ((b & (1 << index)) > 0) ? 1 : 0; }
/// <summary>
/// 从0位起始,设置低index位为1
/// </summary>
/// <param name="b"></param>
/// <param name="index"></param>
/// <returns></returns>
public static byte SetBit(byte b, int index) { return (byte)(b | (1 << index)); }
/// <summary>
/// 从0位起始,设置第index位为0
/// </summary>
/// <param name="b"></param>
/// <param name="index"></param>
/// <returns></returns>
public static byte ClearBit(byte b, int index) { return (byte)(b & (byte.MaxValue - (1 << index))); }
/// <summary>
/// 从0为开始,将第index位取反
/// </summary>
/// <param name="b"></param>
/// <param name="index"></param>
/// <returns></returns>
public static byte ReverseBit(byte b, int index) { return (byte)(b ^ (byte)(1 << index)); }
#endregion
}
}