logo

Converting a float to a 4-byte array

bncastl posted on Monday, October 20, 2008 9: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?
reply


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

kucer posted on Monday, October 20, 2008 1: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.
reply

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

bncastl posted on Monday, October 20, 2008 1: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.
reply

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

Pavel Bansky (MSFT) posted on Monday, October 20, 2008 7: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
reply

I would give it a try, because I think the MF's float serialized is the array

kucer posted on Monday, October 20, 2008 8: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
reply

Converting a float to a 4-byte array

hrrglburf posted on Wednesday, October 22, 2008 2: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
reply

 

Didn't Find The Answer You Were Looking For?

View .NET Microframework Posts   Ask A New Question

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Previous .NET Microframework conversation.

Deployment Server    .NET Distributed Applications    .NET Framework    ADO.NET    ASP.NET    ASP.NET Security    ASP.NET Web Controls    ASP.NET Web Services    .NET CLR    .NET Compact Framework    .NET Drawing GDI+    .NET COM Interop    .NET Microframework    .NET Microframework Porting    .NET Performance    .NET Web Services    .NET Windows Forms    .NET WinForms Controls    .NET    C#    VB.NET    VC++.NET    .NET Security    Xml    JScript    VBScript    WSH    Smart Phone Developer    VB COM    VB Controls    VB Crystal    VB DataBase ADO    VB Syntax    VB Vista Compatibility    VB WinAPI    VC ATL    VC Debugger    VC Language    VC MFC    VC STL    Visio Developer VBA    Visual Studio .NET Debugging    Powershell    WindowsCE Embedded VC    Xml    Xsl   






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010