-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteArray.cs
276 lines (246 loc) · 6.28 KB
/
ByteArray.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Created by SharpDevelop.
* User: C3rebro
* Date: 12.03.2018
* Time: 21:02
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
namespace ByteArrayHelper
{
public class ByteArray
{
public int Length { get { return Data.Length; } }
public byte[] Data { get; set; }
public ByteArray()
{
}
public ByteArray(int length)
{
Data = new byte[length];
}
public ByteArray(byte[] _data)
{
Data = new byte[_data.Length];
Data = _data;
}
public byte[] Or(List<byte[]> source, bool isLittleEndian = true)
{
ObservableCollection<byte[]> localCopy = new ObservableCollection<byte[]>(source);
for (int i = source.Count; i > 0; i--)
{
foreach ( byte[] b in source)
{
}
}
return null;
//localCopy.Aggregate((i, j) => i.MainListWords.Length > j.MainListWords.Length ? i : j)
}
public ByteArray Or(byte[] source, bool isLittleEndian = true)
{
byte[] localCopy = new byte[Data.Length];
if(!isLittleEndian) // or-ing from right to left
{
// for (int i = localCopy.Length; i > 0; i--)
// {
// if((localCopy.Length - source.Length) < i)
// {
// Data[i] |= source[i - ];
// }
// }
}
else // the other way around
{
for (int i = 0; i < localCopy.Length; i++)
{
if(source.Length > i)
{
Data[i] |= source[i];
}
else
break;
}
}
return this;
}
}
namespace Extensions
{
/// <summary>
/// Convert byte and byte[] to and from other types
/// </summary>
public static class ByteConverter
{
/// <summary>
/// Reverses an Array of Bytes from Little Endian to Big Endian or Vice Versa
/// </summary>
/// <param name="arrToReverse">The byte[] Array to be reversed</param>
/// <returns>The byte[] Array in Reversed Order</returns>
public static byte[] Reverse(byte[] arrToReverse)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(arrToReverse);
return arrToReverse;
}
/// <summary>
/// Reverses the Bit Order in a Single Byte
/// </summary>
/// <param name="byteToPutInReverseOrder">The byte to be reversed</param>
/// <returns>The byte in reversed Order</returns>
public static byte Reverse(byte byteToPutInReverseOrder)
{
byte byteInReversedOrder = 0;
for (byte i = 0; i < 8; ++i)
{
byteInReversedOrder <<= 1;
byteInReversedOrder |= (byte)(byteToPutInReverseOrder & 1);
byteToPutInReverseOrder >>= 1;
}
return byteInReversedOrder;
}
/// <summary>
/// Gets the Amount of bytes in a string that contains one or more bytes in the format "00" to "FF" each byte
/// </summary>
/// <param name="hexString">A string containing one or more bytes in the format "00" to "FF" each byte</param>
/// <returns>The Amount of Bytes in the string</returns>
public static int GetByteCount(string hexString)
{
int numHexChars = 0;
char c;
// remove all none A-F, 0-9, characters
for (int i = 0; i < hexString.Length; i++)
{
c = hexString[i];
if (IsHexDigit(c))
numHexChars++;
}
// if odd number of characters, discard last character
if (numHexChars % 2 != 0)
{
numHexChars--;
}
return numHexChars / 2; // 2 characters per byte
}
/// <summary>
/// Converts a string with hexadecimal chars to a byte array (e.g.: "FF00FF" -> byte[3]{0xFF,0x00,0xFF})
/// </summary>
/// <param name="hexString">The string to convert</param>
/// <param name="discarded">The Number of characters in the string that could not be converted to a byte</param>
/// <returns>The Array that contains all converted values. Reliably only if <param name="discarded"/> is returned as 0</returns>
public static byte[] GetBytes(string hexString, out int discarded)
{
discarded = 0;
string newString = "";
char c;
// remove all none A-F, 0-9, characters
for (int i = 0; i < hexString.Length; i++)
{
c = hexString[i];
if (IsHexDigit(c))
newString += c;
else
discarded++;
}
// if odd number of characters, discard last character
if (newString.Length % 2 != 0)
{
discarded++;
newString = newString.Substring(0, newString.Length - 1);
}
int byteLength = newString.Length / 2;
byte[] bytes = new byte[byteLength];
string hex;
int j = 0;
for (int i = 0; i < bytes.Length; i++)
{
hex = new String(new Char[] { newString[j], newString[j + 1] });
bytes[i] = HexToByte(hex);
j = j + 2;
}
return bytes;
}
/// <summary>
///
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string HexToString(byte[] bytes)
{
string hexString = "";
for (int i = 0; i < bytes.Length; i++)
{
hexString += bytes[i].ToString("X2");
}
return hexString;
}
/// <summary>
///
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string HexToString(byte bytes)
{
string hexString = "";
{
hexString += bytes.ToString("X2");
}
return hexString;
}
/// <summary>
///
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static bool IsInHexFormat(string hexString)
{
bool hexFormat = true;
foreach (char digit in hexString)
{
if (!IsHexDigit(digit))
{
hexFormat = false;
break;
}
}
return hexFormat;
}
/// <summary>
///
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static bool IsHexDigit(Char c)
{
int numChar;
int numA = Convert.ToInt32('A');
int num1 = Convert.ToInt32('0');
c = Char.ToUpper(c);
numChar = Convert.ToInt32(c);
if (numChar >= numA && numChar < (numA + 6))
return true;
if (numChar >= num1 && numChar < (num1 + 10))
return true;
return false;
}
private static byte HexToByte(string hex)
{
byte newByte = 0x00;
try
{
if (!(hex.Length > 2 || hex.Length <= 0))
{
newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}
}
catch
{
return 0x00;
}
return newByte;
}
}
}
}