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 // Reverse array if we are on big-endian machine
35 Array.Reverse(resultBytes);
41 public static byte[] LEBytes(uint n)
43 byte[] resultBytes = BitConverter.GetBytes(n);
45 if (!BitConverter.IsLittleEndian)
47 // Reverse array if we are on big-endian machine
48 Array.Reverse(resultBytes);
54 public static byte[] LEBytes(ulong n)
56 byte[] resultBytes = BitConverter.GetBytes(n);
58 if (!BitConverter.IsLittleEndian)
60 // Reverse array if we are on big-endian machine
61 Array.Reverse(resultBytes);
67 public static byte[] BEBytes(ushort n)
69 byte[] resultBytes = BitConverter.GetBytes(n);
71 if (BitConverter.IsLittleEndian)
73 // Reverse array if we are on little-endian machine
74 Array.Reverse(resultBytes);
80 public static byte[] BEBytes(uint n)
82 byte[] resultBytes = BitConverter.GetBytes(n);
84 if (BitConverter.IsLittleEndian)
86 // Reverse array if we are on little-endian machine
87 Array.Reverse(resultBytes);
93 public static byte[] BEBytes(ulong n)
95 byte[] resultBytes = BitConverter.GetBytes(n);
97 if (BitConverter.IsLittleEndian)
99 // Reverse array if we are on little-endian machine
100 Array.Reverse(resultBytes);
106 public static ushort LEBytesToUInt16(byte[] bytes)
108 if (bytes.Length != sizeof(ushort))
110 throw new InteropException("Array size doesn't match the ushort data type.");
113 if (!BitConverter.IsLittleEndian)
115 // Reverse array if we are on big-endian machine
116 Array.Reverse(bytes);
119 return BitConverter.ToUInt16(bytes, 0);
122 public static uint LEBytesToUInt32(byte[] bytes)
124 if (bytes.Length != sizeof(uint))
126 throw new InteropException("Array size doesn't match the uint data type.");
129 if (!BitConverter.IsLittleEndian)
131 // Reverse array if we are on big-endian machine
132 Array.Reverse(bytes);
135 return BitConverter.ToUInt32(bytes, 0);
138 public static ulong LEBytesToUInt64(byte[] bytes)
140 if (bytes.Length != sizeof(ulong))
142 throw new InteropException("Array size doesn't match the ulong data type.");
145 if (!BitConverter.IsLittleEndian)
147 // Reverse array if we are on big-endian machine
148 Array.Reverse(bytes);
151 return BitConverter.ToUInt64(bytes, 0);
154 public static ushort BEBytesToUInt16(byte[] bytes)
156 if (bytes.Length != sizeof(ushort))
158 throw new InteropException("Array size doesn't match the ushort data type.");
161 if (BitConverter.IsLittleEndian)
163 // Reverse array if we are on little-endian machine
164 Array.Reverse(bytes);
167 return BitConverter.ToUInt16(bytes, 0);
170 public static uint BEBytesToUInt32(byte[] bytes)
172 if (bytes.Length != sizeof(uint))
174 throw new InteropException("Array size doesn't match the uint data type.");
177 if (BitConverter.IsLittleEndian)
179 // Reverse array if we are on little-endian machine
180 Array.Reverse(bytes);
183 return BitConverter.ToUInt32(bytes, 0);
186 public static ulong BEBytesToUInt64(byte[] bytes)
188 if (bytes.Length != sizeof(ulong))
190 throw new InteropException("Array size doesn't match the ulong data type.");
193 if (BitConverter.IsLittleEndian)
195 // Reverse array if we are on little-endian machine
196 Array.Reverse(bytes);
199 return BitConverter.ToUInt64(bytes, 0);