2 using System.Collections.Generic;
5 using System.Threading.Tasks;
9 public class InteropException : Exception
11 public InteropException()
15 public InteropException(string message)
20 public InteropException(string message, Exception inner)
21 : base(message, inner)
28 public static byte[] LEBytes(ushort n)
30 byte[] resultBytes = BitConverter.GetBytes(n);
32 if (!BitConverter.IsLittleEndian)
34 Array.Reverse(resultBytes);
40 public static byte[] LEBytes(uint n)
42 byte[] resultBytes = BitConverter.GetBytes(n);
44 if (!BitConverter.IsLittleEndian)
46 Array.Reverse(resultBytes);
52 public static byte[] LEBytes(ulong n)
54 byte[] resultBytes = BitConverter.GetBytes(n);
56 if (!BitConverter.IsLittleEndian)
58 Array.Reverse(resultBytes);
64 public static byte[] BEBytes(ushort n)
66 byte[] resultBytes = BitConverter.GetBytes(n);
68 if (BitConverter.IsLittleEndian)
70 Array.Reverse(resultBytes);
76 public static byte[] BEBytes(uint n)
78 byte[] resultBytes = BitConverter.GetBytes(n);
80 if (BitConverter.IsLittleEndian)
82 Array.Reverse(resultBytes);
88 public static byte[] BEBytes(ulong n)
90 byte[] resultBytes = BitConverter.GetBytes(n);
92 if (BitConverter.IsLittleEndian)
94 Array.Reverse(resultBytes);
100 public static ushort LEBytesToUInt16(byte[] bytes)
102 if (bytes.Length != sizeof(ushort))
104 throw new InteropException("Array size doesn't match the ushort data type.");
107 if (!BitConverter.IsLittleEndian)
109 Array.Reverse(bytes);
112 return BitConverter.ToUInt16(bytes, 0);
115 public static uint LEBytesToUInt32(byte[] bytes)
117 if (bytes.Length != sizeof(ushort))
119 throw new InteropException("Array size doesn't match the uint data type.");
122 if (!BitConverter.IsLittleEndian)
124 Array.Reverse(bytes);
127 return BitConverter.ToUInt32(bytes, 0);
130 public static ulong LEBytesToUInt64(byte[] bytes)
132 if (bytes.Length != sizeof(ushort))
134 throw new InteropException("Array size doesn't match the ulong data type.");
137 if (!BitConverter.IsLittleEndian)
139 Array.Reverse(bytes);
142 return BitConverter.ToUInt64(bytes, 0);