Unfortunately no unsafe code is allowed on NETMF, here is my implementation of the BitConverter class (we sure could use that Code Exchange now ).
public static class BitConverter
{
public static byte[] GetBytes(byte val)
{
return new byte[1] { val };
}
public static byte[] GetBytes(char val)
{
return new byte[2] { (byte)(val & 0xFF), (byte)((val >> 8) & 0xFF) };
}
public static byte[] GetBytes(short val)
{
return new byte[2] { (byte)(val & 0xFF), (byte)((val >> 8) & 0xFF) };
}
public static byte[] GetBytes(ushort val)
{
return new byte[2] { (byte)(val & 0xFF), (byte)((val >> 8) & 0xFF) };
}
public static byte[] GetBytes(int val)
{
return new byte[4] {
(byte)(val & 0xFF),
(byte)((val >> 8) & 0xFF),
(byte)((val >> 16) & 0xFF),
(byte)((val >> 24) & 0xFF) };
}
public static byte[] GetBytes(uint val)
{
return new byte[4] {
(byte)(val & 0xFF),
(byte)((val >> 8) & 0xFF),
(byte)((val >> 16) & 0xFF),
(byte)((val >> 24) & 0xFF) };
}
public static byte[] GetBytes(long val)
{
return new byte[8] {
(byte)(val & 0xFF),
(byte)((val >> 8) & 0xFF),
(byte)((val >> 16) & 0xFF),
(byte)((val >> 24) & 0xFF),
(byte)((val >> 32) & 0xFF),
(byte)((val >> 40) & 0xFF),
(byte)((val >> 48) & 0xFF),
(byte)((val >> 56) & 0xFF)};
}
public static byte[] GetBytes(ulong val)
{
return new byte[8] {
(byte)(val & 0xFF),
(byte)((val >> 8) & 0xFF),
(byte)((val >> 16) & 0xFF),
(byte)((val >> 24) & 0xFF),
(byte)((val >> 32) & 0xFF),
(byte)((val >> 40) & 0xFF),
(byte)((val >> 48) & 0xFF),
(byte)((val >> 56) & 0xFF)};
}
public static byte[] GetBytes(float val)
{
byte[] buffer = new byte[4];
GHIElectronics.NETMF.System.Util.InsertValueIntoArray(val, buffer, 0);
return buffer;
}
public static byte[] GetBytes(double val)
{
byte[] buffer = new byte[4];
GHIElectronics.NETMF.System.Util.InsertValueIntoArray((float)val, buffer, 0);
return buffer;
}
public static char ToChar(byte[] val, int index)
{
return (char)(val[0 + index] << 0 | val[1 + index] << 8);
}
public static short ToInt16(byte[] val, int index)
{
return (short)(
val[0 + index] << 0 |
val[1 + index] << 8);
}
public static int ToInt32(byte[] val, int index)
{
return (
val[0 + index] << 0 |
val[1 + index] << 8 |
val[2 + index] << 16 |
val[3 + index] << 24);
}
public static long ToInt64(byte[] val, int index)
{
return (
val[0 + index] << 0 |
val[1 + index] << 8 |
val[2 + index] << 16 |
val[3 + index] << 24 |
val[4 + index] << 32 |
val[5 + index] << 40 |
val[6 + index] << 48 |
val[7 + index] << 56);
}
public static ushort ToUInt16(byte[] val, int index)
{
return (ushort)(
val[0 + index] << 0 |
val[1 + index] << 8);
}
public static uint ToUInt32(byte[] val,int index)
{
return (uint)(
val[0 + index] << 0 |
val[1 + index] << 8 |
val[2 + index] << 16 |
val[3 + index] << 24);
}
public static ulong ToUInt64(byte[] val,int index)
{
return (ulong)(
val[0 + index] << 0 |
val[1 + index] << 8 |
val[2 + index] << 16 |
val[3 + index] << 24 |
val[4 + index] << 32 |
val[5 + index] << 40 |
val[6 + index] << 48 |
val[7 + index] << 56);
}
public static float ToSingle(byte[] val,int index)
{
float ret;
GHIElectronics.NETMF.System.Util.ExtractValueFromArray(out ret, val, index);
return ret;
}
public static double ToDouble(byte[] val,int index)
{
float ret;
GHIElectronics.NETMF.System.Util.ExtractValueFromArray(out ret, val, index);
return (double)ret;
}
public static string ToString(byte[] val)
{
return new string(System.Text.Encoding.UTF8.GetChars(val));
}
}