Asked By Pavel Bansky (MSFT)
20-Oct-08 07:51 PM

Hi, from my experience I've usually converted the float to integer
multipling by 10 or 100 (whatever). Working with whole numbers and
converting it to array is much faster then working float. Also the
deserialization is possible on every system, not limited to those which
understands .NET serialization.
This is part of my Devantech drivers projects (Endianity class). Set of
functions that converts integer to byte array and vice versa. This
implements big endian codding.
/// <summary>
/// Splits number into the byte array in Big Endian
/// </summary>
/// Number to split
/// Array where the bytes be
stored
public static void ToBigEndian(long number, byte[] outputArray)
{
int length = outputArray.Length;
outputArray[length-1] = (byte)number;
for (int i = length-2; i >= 0; i--)
outputArray[i] = (byte)(number >> (8 * (i+1)));
}
/// <summary>
/// Gets value from array byte organized as Big Endian
/// </summary>
/// Byte array
/// Start index
/// Number of bytes to parse
/// <returns>Long value</returns>
private static long FromBigEndian(byte[] byteArray, int
startIndex, int length)
{
long retValue = 0;
int stopIndex = startIndex+length-1;
for (int i = startIndex; i < (stopIndex); i++)
{
retValue |= byteArray[i];
retValue = retValue << 8;
}
return retValue | byteArray[stopIndex];
}
Pavel Bánský
Microsoft Czech Republic
http://bansky.net/blog