Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
Miscellaneous GroupsView
Adsi General
Axapta
Greatplains
Helpauthoring
Hiserver General
Infopath
Mac Office
Mac Office Entourage
Mac Office Word
Mac Otherproducts
Mac Rdc
Mac Virtualpc
Macintosh General
Mom
Mshardware Product
Windows File_System
Windows Group_Policy
Windows Msi
Windows Tabletpc
Pos
Servicesforunix General
Simulators
Smartphone
Softwareupdatesvcs
Solomon
Windbg
Windows Terminal_Services
Dataprotectionmanager
Axapta Application
Axapta Programming
Windows Sharedaccess
Fortran
Groove
Msroboticsstudio
Deployment Desktop
Design Gallery
Developer Outlook Addins
Development Device Drivers
Windows Devices Pnpx
.NET Micro
Dynamics Gp Developer
Security Forefront
Internetexplorer Beta
Sv Windows
Development Device Drivers Dtm
Hyper-V
Internetexplorer

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Apps
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft NET Micro Posts  Ask A New Question 

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

 
 

Previous Microsoft NET Micro conversation.