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[] LEBytes(ushort n)
29 byte[] resultBytes = BitConverter.GetBytes(n);
31 if (!BitConverter.IsLittleEndian)
33 // Reverse array if we are on big-endian machine
34 Array.Reverse(resultBytes);
40 public static byte[] LEBytes(uint n)
42 byte[] resultBytes = BitConverter.GetBytes(n);
44 if (!BitConverter.IsLittleEndian)
46 // Reverse array if we are on big-endian machine
47 Array.Reverse(resultBytes);
53 public static byte[] LEBytes(ulong n)
55 byte[] resultBytes = BitConverter.GetBytes(n);
57 if (!BitConverter.IsLittleEndian)
59 // Reverse array if we are on big-endian machine
60 Array.Reverse(resultBytes);
66 public static byte[] BEBytes(ushort n)
68 byte[] resultBytes = BitConverter.GetBytes(n);
70 if (BitConverter.IsLittleEndian)
72 // Reverse array if we are on little-endian machine
73 Array.Reverse(resultBytes);
79 public static byte[] BEBytes(uint n)
81 byte[] resultBytes = BitConverter.GetBytes(n);
83 if (BitConverter.IsLittleEndian)
85 // Reverse array if we are on little-endian machine
86 Array.Reverse(resultBytes);
92 public static byte[] BEBytes(ulong n)
94 byte[] resultBytes = BitConverter.GetBytes(n);
96 if (BitConverter.IsLittleEndian)
98 // Reverse array if we are on little-endian machine
99 Array.Reverse(resultBytes);
105 public static ushort LEBytesToUInt16(byte[] bytes)
107 if (bytes.Length != sizeof(ushort))
109 throw new InteropException("Array size doesn't match the ushort data type.");
112 if (!BitConverter.IsLittleEndian)
114 // Reverse array if we are on big-endian machine
115 Array.Reverse(bytes);
118 return BitConverter.ToUInt16(bytes, 0);
121 public static uint LEBytesToUInt32(byte[] bytes)
123 if (bytes.Length != sizeof(uint))
125 throw new InteropException("Array size doesn't match the uint data type.");
128 if (!BitConverter.IsLittleEndian)
130 // Reverse array if we are on big-endian machine
131 Array.Reverse(bytes);
134 return BitConverter.ToUInt32(bytes, 0);
137 public static ulong LEBytesToUInt64(byte[] bytes)
139 if (bytes.Length != sizeof(ulong))
141 throw new InteropException("Array size doesn't match the ulong data type.");
144 if (!BitConverter.IsLittleEndian)
146 // Reverse array if we are on big-endian machine
147 Array.Reverse(bytes);
150 return BitConverter.ToUInt64(bytes, 0);
153 public static ushort BEBytesToUInt16(byte[] bytes)
155 if (bytes.Length != sizeof(ushort))
157 throw new InteropException("Array size doesn't match the ushort data type.");
160 if (BitConverter.IsLittleEndian)
162 // Reverse array if we are on little-endian machine
163 Array.Reverse(bytes);
166 return BitConverter.ToUInt16(bytes, 0);
169 public static uint BEBytesToUInt32(byte[] bytes)
171 if (bytes.Length != sizeof(uint))
173 throw new InteropException("Array size doesn't match the uint data type.");
176 if (BitConverter.IsLittleEndian)
178 // Reverse array if we are on little-endian machine
179 Array.Reverse(bytes);
182 return BitConverter.ToUInt32(bytes, 0);
185 public static ulong BEBytesToUInt64(byte[] bytes)
187 if (bytes.Length != sizeof(ulong))
189 throw new InteropException("Array size doesn't match the ulong data type.");
192 if (BitConverter.IsLittleEndian)
194 // Reverse array if we are on little-endian machine
195 Array.Reverse(bytes);
198 return BitConverter.ToUInt64(bytes, 0);
201 public static IEnumerable<byte> ParseHex(string hex)
203 return Enumerable.Range(0, hex.Length)
204 .Where(x => x % 2 == 0)
205 .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
208 public static string ToHex(IEnumerable<byte> bytes)
210 StringBuilder sb = new StringBuilder();
211 foreach (byte b in bytes)
213 sb.AppendFormat("{0:x2}", b);
215 return sb.ToString();