Microsoft.SPOT.Reflection.Serialize
(1)
GetBytes
(1)
Array
(1)
FromBigEndian
(1)
ToBigEndian
(1)
Serialize
(1)
ADCs
(1)
OutputArray.Length
(1)

Converting a float to a 4-byte array

Asked By bncastl
20-Oct-08 09:54 AM
Hi,

I need to convert a float to an array of bytes.  I've tried using unsafe
code and casting the float to get what I need.

public static unsafe byte[] GetBytes(int value)
{
byte[] buffer = new byte[4];
fixed (byte* numRef = buffer)
{
*((int*)numRef) = value;
}

return buffer;
}

public static unsafe byte[] GetBytes(float value)
{
return GetBytes(*((int*)&value));
}

It compiles, but I get an exception when I try to run it. Any other way to
do this that I'm missing?

You can't use unsafe code in .NET Micro Framework.

Asked By kucer
20-Oct-08 01:01 PM
You can't use unsafe code in .NET Micro Framework.
In case of float, you can use the serialization to do the trick:

Microsoft.SPOT.Reflection.Serialize(f, typeof(float));

Just curious... what do you need it for?

Jan

PS. The serialization does not guarantee that it will return what you expect
to get using unsafe code on PC.

Ah. That certainly explains it. Thanks.What do I need it for?

Asked By bncastl
20-Oct-08 01:24 PM
Ah. That certainly explains it.  Thanks.

What do I need it for?

I have an array of floats (voltage values readback from ADCs) that I want to
package up and send over the network.  I certainly could (and currently am)
sending the raw ushort values over and having the client program convert the
data.  This however, requires the client to know the conversion factor (the
ADC ref voltage and the ADC's # of bits). If either of those parameters were
to ever change, say if I had to spec a different ADC on my board, then I
would have to change the client software and also have a way of
distinguishing old vs new boards - unless I do the conversion on the embedded
micro then ship the "float bytes" over to the client.

Hi, from my experience I've usually converted the float to integer multipling

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
I would give it a try, because I think the MF's float serialized is the array
Asked By kucer
20-Oct-08 08:01 PM
I would give it a try, because I think the MF's float serialized is the
array you are looking for, maybe you would just have to reverse it (you
know, the endian stuff)... And there is not much more efficient way how to
store a float so it is a good chance this will work on future versions too
(again, not guaranteed).

Jan
Converting a float to a 4-byte array
Asked By hrrglburf
22-Oct-08 02:54 AM
e
o

We're using Aevi Source on our projects, it is got a bunch of utils,
including GetBytes for floats/singles, handles big and little endian
too.
it is at aevirobotics.com
Post Question To EggHeadCafe