2 using System.Collections.Generic;
8 public class InteropException : Exception
10 public InteropException()
14 public InteropException(string message)
19 public InteropException(string message, Exception inner)
20 : base(message, inner)
27 public static byte[] ReverseIfLE(byte[] source)
29 if (BitConverter.IsLittleEndian)
31 Array.Reverse(source);
37 public static byte[] LEBytes(uint[] values)
39 if (BitConverter.IsLittleEndian)
41 byte[] result = new byte[values.Length * sizeof(uint)];
42 Buffer.BlockCopy(values, 0, result, 0, result.Length);
48 List<byte> result = new List<byte>();
50 foreach (uint i in values)
52 result.AddRange(LEBytes(i));
55 return result.ToArray();
59 public static uint[] ToUInt32Array(byte[] bytes)
61 if (BitConverter.IsLittleEndian)
63 uint[] result = new uint[bytes.Length / sizeof(uint)];
64 Buffer.BlockCopy(bytes, 0, result, 0, result.Length);
70 List<uint> result = new List<uint>();
72 for (int i = 0; i < bytes.Length; i += 4)
74 result.Add(LEBytesToUInt32(bytes.Skip(i).Take(4).ToArray()));
77 return result.ToArray();
82 public static byte[] LEBytes(ushort n)
84 byte[] resultBytes = BitConverter.GetBytes(n);
86 if (!BitConverter.IsLittleEndian)
88 // Reverse array if we are on big-endian machine
89 Array.Reverse(resultBytes);
95 public static byte[] LEBytes(uint n)
97 byte[] resultBytes = BitConverter.GetBytes(n);
99 if (!BitConverter.IsLittleEndian)
101 // Reverse array if we are on big-endian machine
102 Array.Reverse(resultBytes);
108 public static byte[] LEBytes(ulong n)
110 byte[] resultBytes = BitConverter.GetBytes(n);
112 if (!BitConverter.IsLittleEndian)
114 // Reverse array if we are on big-endian machine
115 Array.Reverse(resultBytes);
121 public static byte[] BEBytes(ushort n)
123 byte[] resultBytes = BitConverter.GetBytes(n);
125 if (BitConverter.IsLittleEndian)
127 // Reverse array if we are on little-endian machine
128 Array.Reverse(resultBytes);
134 public static byte[] BEBytes(uint n)
136 byte[] resultBytes = BitConverter.GetBytes(n);
138 if (BitConverter.IsLittleEndian)
140 // Reverse array if we are on little-endian machine
141 Array.Reverse(resultBytes);
147 public static byte[] BEBytes(ulong n)
149 byte[] resultBytes = BitConverter.GetBytes(n);
151 if (BitConverter.IsLittleEndian)
153 // Reverse array if we are on little-endian machine
154 Array.Reverse(resultBytes);
160 public static ushort LEBytesToUInt16(byte[] bytes)
162 if (bytes.Length != sizeof(ushort))
164 throw new InteropException("Array size doesn't match the ushort data type.");
167 if (!BitConverter.IsLittleEndian)
169 // Reverse array if we are on big-endian machine
170 Array.Reverse(bytes);
173 return BitConverter.ToUInt16(bytes, 0);
176 public static uint LEBytesToUInt32(byte[] bytes)
178 if (bytes.Length != sizeof(uint))
180 throw new InteropException("Array size doesn't match the uint data type.");
183 if (!BitConverter.IsLittleEndian)
185 // Reverse array if we are on big-endian machine
186 Array.Reverse(bytes);
189 return BitConverter.ToUInt32(bytes, 0);
192 public static ulong LEBytesToUInt64(byte[] bytes)
194 if (bytes.Length != sizeof(ulong))
196 throw new InteropException("Array size doesn't match the ulong data type.");
199 if (!BitConverter.IsLittleEndian)
201 // Reverse array if we are on big-endian machine
202 Array.Reverse(bytes);
205 return BitConverter.ToUInt64(bytes, 0);
208 public static ushort BEBytesToUInt16(byte[] bytes)
210 if (bytes.Length != sizeof(ushort))
212 throw new InteropException("Array size doesn't match the ushort data type.");
215 if (BitConverter.IsLittleEndian)
217 // Reverse array if we are on little-endian machine
218 Array.Reverse(bytes);
221 return BitConverter.ToUInt16(bytes, 0);
224 public static uint BEBytesToUInt32(byte[] bytes)
226 if (bytes.Length != sizeof(uint))
228 throw new InteropException("Array size doesn't match the uint data type.");
231 if (BitConverter.IsLittleEndian)
233 // Reverse array if we are on little-endian machine
234 Array.Reverse(bytes);
237 return BitConverter.ToUInt32(bytes, 0);
240 public static ulong BEBytesToUInt64(byte[] bytes)
242 if (bytes.Length != sizeof(ulong))
244 throw new InteropException("Array size doesn't match the ulong data type.");
247 if (BitConverter.IsLittleEndian)
249 // Reverse array if we are on little-endian machine
250 Array.Reverse(bytes);
253 return BitConverter.ToUInt64(bytes, 0);
256 public static IEnumerable<byte> ParseHex(string hex)
258 return Enumerable.Range(0, hex.Length)
259 .Where(x => x % 2 == 0)
260 .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
263 public static string ToHex(IEnumerable<byte> bytes)
265 StringBuilder sb = new StringBuilder();
266 foreach (byte b in bytes)
268 sb.AppendFormat("{0:x2}", b);
270 return sb.ToString();