if (!BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on big-endian machine
Array.Reverse(resultBytes);
}
if (!BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on big-endian machine
Array.Reverse(resultBytes);
}
if (!BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on big-endian machine
Array.Reverse(resultBytes);
}
if (BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on little-endian machine
Array.Reverse(resultBytes);
}
if (BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on little-endian machine
Array.Reverse(resultBytes);
}
if (BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on little-endian machine
Array.Reverse(resultBytes);
}
if (!BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on big-endian machine
Array.Reverse(bytes);
}
public static uint LEBytesToUInt32(byte[] bytes)
{
- if (bytes.Length != sizeof(ushort))
+ if (bytes.Length != sizeof(uint))
{
throw new InteropException("Array size doesn't match the uint data type.");
}
if (!BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on big-endian machine
Array.Reverse(bytes);
}
public static ulong LEBytesToUInt64(byte[] bytes)
{
- if (bytes.Length != sizeof(ushort))
+ if (bytes.Length != sizeof(ulong))
{
throw new InteropException("Array size doesn't match the ulong data type.");
}
if (!BitConverter.IsLittleEndian)
{
+ // Reverse array if we are on big-endian machine
Array.Reverse(bytes);
}
return BitConverter.ToUInt64(bytes, 0);
}
+ public static ushort BEBytesToUInt16(byte[] bytes)
+ {
+ if (bytes.Length != sizeof(ushort))
+ {
+ throw new InteropException("Array size doesn't match the ushort data type.");
+ }
+
+ if (BitConverter.IsLittleEndian)
+ {
+ // Reverse array if we are on little-endian machine
+ Array.Reverse(bytes);
+ }
+
+ return BitConverter.ToUInt16(bytes, 0);
+ }
+
+ public static uint BEBytesToUInt32(byte[] bytes)
+ {
+ if (bytes.Length != sizeof(uint))
+ {
+ throw new InteropException("Array size doesn't match the uint data type.");
+ }
+
+ if (BitConverter.IsLittleEndian)
+ {
+ // Reverse array if we are on little-endian machine
+ Array.Reverse(bytes);
+ }
+
+ return BitConverter.ToUInt32(bytes, 0);
+ }
+
+ public static ulong BEBytesToUInt64(byte[] bytes)
+ {
+ if (bytes.Length != sizeof(ulong))
+ {
+ throw new InteropException("Array size doesn't match the ulong data type.");
+ }
+
+ if (BitConverter.IsLittleEndian)
+ {
+ // Reverse array if we are on little-endian machine
+ Array.Reverse(bytes);
+ }
+
+ return BitConverter.ToUInt64(bytes, 0);
+ }
}
}