-
Notifications
You must be signed in to change notification settings - Fork 7
/
ByteArrayUtil.cs
198 lines (164 loc) · 6.76 KB
/
ByteArrayUtil.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
using System;
using System.Text;
namespace MavLink
{
internal static class ByteArrayUtil
{
#if MF_FRAMEWORK_VERSION_V4_1
private static readonly MavBitConverter bitConverter = new MavBitConverter();
#else
private static readonly FrameworkBitConverter bitConverter = new FrameworkBitConverter();
#endif
public static byte[] ToChar(byte[] source, int sourceOffset, int size)
{
var bytes = new byte[size];
for (int i = 0; i < size; i++)
bytes[i] = source[i + sourceOffset];
return bytes;
}
public static byte[] ToUInt8(byte[] source, int sourceOffset, int size)
{
var bytes = new byte[size];
Array.Copy(source, sourceOffset, bytes, 0, size);
return bytes;
}
public static sbyte[] ToInt8(byte[] source, int sourceOffset, int size)
{
var bytes = new sbyte[size];
for (int i = 0; i < size; i++)
bytes[i] = unchecked((sbyte)source[i + sourceOffset]);
return bytes;
}
public static UInt16[] ToUInt16(byte[] source, int sourceOffset, int size)
{
var arr = new UInt16[size];
for (int i = 0; i < size; i++)
arr[i] = bitConverter.ToUInt16(source, sourceOffset + (i * sizeof (UInt16)));
return arr;
}
public static Int16[] ToInt16(byte[] source, int sourceOffset, int size)
{
var arr = new Int16[size];
for (int i = 0; i < size; i++)
arr[i] = bitConverter.ToInt16(source, sourceOffset + (i * sizeof(Int16)));
return arr;
}
public static UInt32[] ToUInt32(byte[] source, int sourceOffset, int size)
{
var arr = new UInt32[size];
for (int i = 0; i < size; i++)
arr[i] = bitConverter.ToUInt32(source, sourceOffset + (i * sizeof(UInt32)));
return arr;
}
public static Int32[] ToInt32(byte[] source, int sourceOffset, int size)
{
var arr = new Int32[size];
for (int i = 0; i < size; i++)
arr[i] = bitConverter.ToInt32(source, sourceOffset + (i * sizeof(Int32)));
return arr;
}
public static UInt64[] ToUInt64(byte[] source, int sourceOffset, int size)
{
var arr = new UInt64[size];
for (int i = 0; i < size; i++)
arr[i] = bitConverter.ToUInt64(source, sourceOffset + (i * sizeof(UInt64)));
return arr;
}
public static Int64[] ToInt64(byte[] source, int sourceOffset, int size)
{
var arr = new Int64[size];
for (int i = 0; i < size; i++)
arr[i] = bitConverter.ToInt64(source, sourceOffset + (i * sizeof(Int64)));
return arr;
}
public static Single[] ToSingle(byte[] source, int sourceOffset, int size)
{
var arr = new Single[size];
for (int i = 0; i < size; i++)
arr[i] = bitConverter.ToSingle(source, sourceOffset + (i * sizeof(Single)));
return arr;
}
public static Double[] ToDouble(byte[] source, int sourceOffset, int size)
{
var arr = new Double[size];
for (int i = 0; i < size; i++)
arr[i] = bitConverter.ToDouble(source, sourceOffset + (i * sizeof(Double)));
return arr;
}
public static void ToByteArray(byte[] src, byte[] dst, int offset, int size)
{
int i;
for (i = 0; i < src.Length; i++)
dst[offset + i] = src[i];
while (i++ < size)
dst[offset + i] = 0;
}
public static void ToByteArray(sbyte[] src, byte[] dst, int offset, int size)
{
int i;
for (i = 0; i < size && i<src.Length; i++)
dst[offset + i] = (byte)src[i];
while (i++ < size)
dst[offset + i] = 0;
}
public static void ToByteArray(UInt16[] src, byte[] dst, int offset, int size)
{
for (int i = 0; i < size && i < src.Length; i++)
bitConverter.GetBytes(src[i], dst, offset + (i*sizeof (UInt16)));
}
public static void ToByteArray(Int16[] src, byte[] dst, int offset, int size)
{
for (int i = 0; i < size && i < src.Length; i++)
bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Int16)));
}
public static void ToByteArray(Int32[] src, byte[] dst, int offset, int size)
{
for (int i = 0; i < size && i < src.Length; i++)
bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Int32)));
}
public static void ToByteArray(UInt32[] src, byte[] dst, int offset, int size)
{
for (int i = 0; i < size && i < src.Length; i++)
bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(UInt32)));
}
public static void ToByteArray(Single[] src, byte[] dst, int offset, int size)
{
for (int i = 0; i < size && i < src.Length; i++)
bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Single)));
}
public static void ToByteArray(Double[] src, byte[] dst, int offset, int size)
{
for (int i = 0; i < size && i < src.Length; i++)
bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Double)));
}
public static void ToByteArray(UInt64[] src, byte[] dst, int offset, int size)
{
for (int i = 0; i < size && i < src.Length; i++)
bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(UInt64)));
}
public static void ToByteArray(Int64[] src, byte[] dst, int offset, int size)
{
for (int i = 0; i < size && i < src.Length; i++)
bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Int64)));
}
public static string ToString(sbyte[] sbytes)
{
var bytes = new byte[sbytes.Length];
int i;
for ( i = 0; i < bytes.Length && sbytes[i] != '\0'; i++)
bytes[i] = (byte) sbytes[i];
var bytesUntilNull = new byte[i];
Array.Copy(bytes, bytesUntilNull, i);
var encoding = new UTF8Encoding();
return new string(encoding.GetChars(bytesUntilNull));
}
public static string ToString(byte[] bs)
{
int i;
for (i = 0; i < bs.Length && bs[i] != '\0'; i++);
var bytesUntilNull = new byte[i];
Array.Copy(bs, bytesUntilNull, i);
return new string(new UTF8Encoding().GetChars(bytesUntilNull));
}
}
}