-
Notifications
You must be signed in to change notification settings - Fork 6
/
Util.cs
176 lines (148 loc) · 5.25 KB
/
Util.cs
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
176
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Reflection;
namespace x86CS
{
public static class Util
{
public static int CountSet(this BitArray bits)
{
int count = 0;
for (int i = 0; i < bits.Length; i++)
{
if (bits[i])
count++;
}
return count;
}
public static byte GetLow(this byte b)
{
return (byte)(b & 0x0f);
}
public static byte SetLow(this byte b, byte value)
{
return (byte)((b & 0xf0) + (value & 0x0f));
}
public static byte SetHigh(this byte b, byte value)
{
return (byte)((value.GetLow() << 4) + b.GetLow());
}
public static byte GetHigh(this byte b)
{
return (byte)((b >> 4) & 0x0f);
}
public static ushort GetLow(this ushort b)
{
return (ushort)(b & 0x00ff);
}
public static ushort SetLow(this ushort b, ushort value)
{
return (byte)((b & 0xff00) + (value & 0x00ff));
}
public static ushort SetHigh(this ushort b, ushort value)
{
return (ushort)((value.GetLow() << 8) + b.GetLow());
}
public static ushort GetHigh(this ushort b)
{
return (ushort)((b >> 8) & 0x00ff);
}
public static byte ToBCD(int value)
{
int tens = value / 10;
int ones = value % 10;
var ret = (byte)(((byte)tens << 4) + (byte)ones);
return ret;
}
public static T ByteArrayToStructure<T>(byte[] bytes) where T : struct
{
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
T structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
handle.Free();
return structure;
}
public static T ByteArrayToStructureBigEndian<T>(byte[] bytes) where T : struct
{
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
T structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
handle.Free();
System.Type t = structure.GetType();
FieldInfo[] fieldInfo = t.GetFields();
foreach (FieldInfo fi in fieldInfo)
{
if (fi.FieldType == typeof(System.Int16))
{
// TODO
}
else if (fi.FieldType == typeof(System.Int32))
{
// TODO
}
else if (fi.FieldType == typeof(System.Int64))
{
// TODO
}
else if (fi.FieldType == typeof(System.UInt16))
{
UInt16 num = (UInt16)fi.GetValue(structure);
byte[] tmp = BitConverter.GetBytes(num);
Array.Reverse(tmp);
fi.SetValueDirect(__makeref(structure), BitConverter.ToUInt16(tmp, 0));
}
else if (fi.FieldType == typeof(System.UInt32))
{
UInt32 num = (UInt32)fi.GetValue(structure);
byte[] tmp = BitConverter.GetBytes(num);
Array.Reverse(tmp);
fi.SetValueDirect(__makeref(structure), BitConverter.ToUInt32(tmp, 0));
}
else if (fi.FieldType == typeof(System.UInt64))
{
UInt64 num = (UInt64)fi.GetValue(structure);
byte[] tmp = BitConverter.GetBytes(num);
Array.Reverse(tmp);
fi.SetValueDirect(__makeref(structure), BitConverter.ToUInt64(tmp, 0));
}
}
return structure;
}
public static uint SwapByteOrder(uint source)
{
uint dest;
dest = (uint)(((source & 0xff) << 24) |
((source & 0xff00) << 8) |
((source & 0xff0000) >> 8) |
((source & 0xff000000) >> 24));
return dest;
}
public static ushort SwapByteOrder(ushort source)
{
ushort dest;
dest = (ushort)(((byte)(source << 8)) | ((byte)(source >> 8)));
return dest;
}
public static void ByteArrayToUShort(byte[] source, ushort[] dest, int index, bool bswap)
{
for (int i = 0, j = index; i < source.Length; i += 2, j++)
{
if(bswap)
dest[j] = (ushort)((source[i] << 8) + source[i + 1]);
else
dest[j] = (ushort)((source[i + 1] << 8) + source[i]);
}
}
public static void ByteArrayToUShort(byte[] source, ushort[] dest, int index)
{
Util.ByteArrayToUShort(source, dest, index, false);
}
public static void UShortArrayToByte(ushort[] source, byte[] dest, int index)
{
for (int i = 0, j = index; i < dest.Length; i += 2, j++)
{
dest[i] = (byte)source[j];
dest[i + 1] = (byte)(source[j] >> 8);
}
}
}
}