Main Site Documentation

Convert Float to 4 byte array?


Is there a way to convert a 32 bit float into it’s 4 bytes of data and vise versa?



I do not think it is possible. This question came up recently and we are looking into options


Thanks for looking into it.

I needed a way to send a float via the SPI and it requires bytes to send over the interface as you know.



Here is what I found that seems to work…

using System;

float x = 1.234F;
// serialization of floats
byte[] buffer = Reflection.Serialize(x, typeof(float));

float y = (float)Reflection.Deserialize(buffer, typeof(float));



Serialization is not supported on USBizi(FEZ) so this will not work


Interesting, well I just coded it in a test program running the emulator and it seems to work.

I have not ran it on the FEZ Domino yet.

If it works in the emulator then wouldn’t it work on the FEZ?
I didn’t see any error messages.

Can you explain?



Emulator is not the same as FEZ. FEZ can read files from a USB thumb drive but the emulator can’t …FEZ supports CAN bus but eh emulator can’t :wink:

Both run the same core but when it comes to libraries, an emulator may have same or different features that the device itself. Of course anyone can build their own emulator to match the hardware. We do not provide a “FEZ” emulator because you probably should emulate your end product (that is based on USBizi) and not he developed board.


So if I run serialization in FEZ Domino I will get an error message?




You will get a not supported exception


We will add something in the future but to get you going now, you can use this:

double number = 3.14159;

// This array is actually the number in text format
byte[] bytes = Encoding.UTF8.GetBytes(number.ToString());

// Get the original number
double originalNumber = double.Parse(new string(Encoding.UTF8.GetChars(bytes)));


Well, that method does not provide the result as I need the IEE754 4 byte format of the float.

I look forward to your future update to support it.



Yes correct but is a way to transfer float in an array. The disadvantage that this method of conversion is slow but should do any task that you may have in mind.