ConverHelper.cs
5.05 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
using System;
using System.Text;
namespace Hh.Mes.Common.Json
{
public static class ConverHelper
{
#region 字符串和Byte之间的转化
/// <summary>
/// 数字和字节之间互转
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static int IntToBitConverter(int num)
{
int temp = 0;
byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组
temp = BitConverter.ToInt32(bytes, 0);//将字节数组内容再转成int32类型
return temp;
}
/// <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;
}
public static string ASCIIToString(short[] ints)
{
String str = "";
foreach (var item in ints)
{
str += Chr(item);
}
return str;
}
public static string Chr(int asciiCode)
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
public static byte[] StringToASCII(string str)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
return asciiEncoding.GetBytes(str.PadRight(20, ' '));
}
public static short Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
short intAsciiCode = (short)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
#endregion
#region 字节位操作
//index从0开始
//获取取第index位
public static int GetBit(byte b, int index) { return ((b & (1 << index)) > 0) ? 1 : 0; }
//将第index位设为1
public static byte SetBit(byte b, int index) { return (byte)(b | (1 << index)); }
//将第index位设为0
public static byte ClearBit(byte b, int index) { return (byte)(b & (byte.MaxValue - (1 << index))); }
//将第index位取反
public static byte ReverseBit(byte b, int index) { return (byte)(b ^ (byte)(1 << index)); }
#endregion
}
}