 integer bits to floating number conversion problem

Hi fellas

I have got troublesome problem during writing some code on arduino. I am trying to send serial floating number to Arduino Mega from Simulink Instrumental Control Toolbox(To instrument block) and process this floting number inside Arduino and get floating number result, and after that send back the result as floating number back to Simulink environment through Query instrument(Serial receive)
In order to do that, I set sending and receiving parts parameters as: format binary and precision 32 bits float with 9600 baudrate. On Arduino I intend to read four bytes which yields to 32 bits inside a for loop and convert them to integer bits as depicted below.

byte inData;

void setup()
{
Serial.begin(9600);
}

void loop()
{

if (Serial.available()) {
for(int i=0; i<4; i++){
}
}
int intbit =0;
intbit = (inData << 24) | ((inData & 0xff) << 16) | ((inData & 0xff) << 8) | (inData & 0xff);
float num = Float.intBitsToFloat(intbit);

////////
// Here some calculations
result = 5*num;
///////

serialFloatPrint(result);
delay(100);
}

void serialFloatPrint(float f) {
byte * b = (byte *) &f;
Serial.write(b);
Serial.write(b);
Serial.write(b);
Serial.write(b);

}

My question is there are various examples are already implemented on programming languages like java and processing that binery to floating number conversions are realized by

float f = Float.intBitsToFloat(number)

How can i realize the same conversion on Arduino IDE. I appreciate your contributions and efforts. Thanks in advance.

(inData << 24)

An eight bit value shifted left 24 bits in a sixteen bit representation is going to be zero.
Some casts are needed.

You need to put the bytes in a byte array which can be cast to a float (ie its a union of float and byte[])

See, for instance, http://forum.arduino.cc/index.php?topic=199604.0

if (Serial.available()) {
for(int i=0; i<4; i++){
}
}

If there is at least one byte to read, read all 4 of them. I’d ask how that’s working for you, but I already know. Not well.

Hi PaulS,

Serial.read() could read 1 byte so i need to read 4 bytes to reunion the 32 bits number ? Isn't it true ?

It's true you need to read four bytes, but it best to wait until you have a byte to read, before reading it.

If you want to read all 4 bytes at once:

if (Serial.available() >= 4)
{
for(int i=0; i<4; i++)
{
}
}

(properly indented, with { where they belong)

Thank you very much i got the point on reading bytes issue. But i got one more question. Sorry for my lack of programming knowledge. How can I interprete binary data that i have previously converted from float data, to float data again on arduino. Suppose that ? have read 32 bit data. How could I convert to float again. :). I mean how to realize Float.intBitsToFloat code on Arduino or other algorithm which is identical. You know that this code is working on Processing, Java, etc.. languages. I really appreciate your efforts. Thanks alot.

I mean how to realize Float.intBitsToFloat code on Arduino

Feel free to find the source code for that method and do the same stuff on the Arduino.

The simplest method, as has already been pointed out, is to use a union.

union
{
byte b;
float f;
} rats;

if(Serial.available() >= 4)
{
for(byte i=0; i<4; i++)
{