Sensor Hex Readout convert to float

I am interfacing between a dynament ethylene sensor and the Arduino Mega.
The communication works great, I already got the sensor data package in the right arrangement of a 4 byte array.
exp. 3F,C7,AE,14 = When I try a internet hex to float converter, it results 1.560000

Can anyone give me a hint how to convert the 4 byte HEX to a float in the arduino sketch, to show it on a screen ?

thx,

byte empty[15]; // read Sensor
byte myBytes[7]={
  0x10, 0x13, 0x06, 0x10, 0x1F, 0x00, 0x58}; // send request to sensor
int a = 0; // counter
byte dataread[4]; // data byte in reverse order
byte result=0;
void setup(void)
{
  Serial.begin(38400);
  Serial1.begin(38400);
  Serial1.flush();
}
void loop(void)
{
  Serial1.write(myBytes, sizeof(myBytes)); // request data from sensor
  if(Serial1.available()){ 
    while(Serial1.available()){
      result=Serial1.read();
      empty[a]=result;
      a++;
      /*Device response on success: DLE, DAT, Data length, Data, DLE, EOF, Checksum High byte, Checksum low byte, i.e. 
       0x10 DLE 
       0x1A  DAT 
       0x08 Data length 
       0x01, 0x00 Version 
       0x00, 0x00  Status flags 
       0x00, 0x00, 0x60, 0x40 Gas reading, 32 bit floating point - IEEE-754 format ( 3.5 ) 
       0x10 DLE 
       0x1F  EOF 
       0x01 Checksum high byte 
       0x02 Checksum low byte         
       Note 1: 0x40600000 = 3.50       
       */      
    }
    // reorder income byte from sensor data
    dataread[0] = empty[10];  
    dataread[1] = empty[9]; 
    dataread[2] = empty[8]; 
    dataread[3] = empty[7]; 
    Serial.print(dataread[0],HEX);
    Serial.print("-");
    Serial.print(dataread[1],HEX);
    Serial.print("-");
    Serial.print(dataread[2],HEX);
    Serial.print("-");
    Serial.println(dataread[3],HEX);
  }  
  delay(1000);  
}
  Serial.begin (115200);
  float pi =  M_PI;
  byte* hexp = (byte*) π
  for (int i = 0; i < 4; i++)
  {
    if (hexp[i] < 16) {
      Serial.print ('0');
    }
    Serial.println ((int)hexp[i], HEX);
  }

You can use a "union" to convert byte arrays to float variables and vice versa, as described here: How to convert a float to a 4 byte char in C? - Stack Overflow
Beware that the byte ordering within a float has two possibilities, depending on which type of processor is involved.
More info for Arduino here: Arduino Playground - IEEE754tools

I tried the " float output" what works on the test_data[] and seems like a quick way to do, but only if the bytes got the "0x" prefix. When I readout the sensor, the bytes come in and are saved without "0x" prefix in the empty[], what makes the float output thing don't work anymore.

The -Union- I could not get run yet.

Any chance to maybe add the "0x" prefix to the byte array ?
thx a lot

byte empty[14]; // sensor readout value
byte myBytes[7]={
  0x10, 0x13, 0x06, 0x10, 0x1F, 0x00, 0x58}; // send request to sensor
int a = 0; // counter
byte dataread[4]; // to manage sensor value
float output;

byte test_data[4]={
  0x40,0x60,0x00,0x00}; // test row only  0x40600000 = 3.50   

void setup(void)
{
  Serial.begin(38400);
  Serial1.begin(38400);
  Serial1.flush();
}

void loop(void)
{

  Serial1.write(myBytes, sizeof(myBytes)); // request data from sensor
  if(Serial1.available()){ 
    while(Serial1.available()){      
      empty[a]=Serial1.read(); // empty collects data from sensor
      a++;      
      Serial.flush();
    }

    dataread[0] = test_data[3];//empty[10];  
    dataread[1] = test_data[2];//empty[9]; 
    dataread[2] = test_data[1];//empty[8]; 
    dataread[3] = test_data[0];//empty[7]; 

    float output = *((float *)dataread);

    Serial.println(output,12);    
    delay(1000);  
  }
}

the bytes come in and are saved without "0x" prefix in the empty[]

The "0x" prefix is a way to inform the C/C++ compiler (in the Arduino environment) that the following digits IN YOUR CODE are hexadecimal, not decimal.

For example the two statements:

variable1 = 23;
variable2 = 0x23;

are not the same. In the Arduino itself a number is stored as binary, which can be interpreted in many different ways.

I tried the " float output" what works on the test_data[] and seems like a quick way to do, but only if the bytes got the "0x" prefix. When I readout the sensor, the bytes come in and are saved without "0x" prefix in the empty[], what makes the float output thing don't work anymore.

I don't understand this. Try to be more clear about what you did and what went wrong. Post some examples of the program output.

I got it run for now. It seems like the problem was in the order of bytes, for some reason they already came in the right order from the sensor, and I reordered wrong. Caused a very small float, which I didn't recognize with 7 dec points only.

If there are any sugestions for correction or constructive criticism, please reply.
For all who have the same problem, feel free use this sketch and improve it.
Thx to the forum.

/*  Sketch by Drees Oellerich (2014) 
    Readout Dynament Ethylene Sensor 
*/

byte empty[15]; // read Sensor
byte myBytes[7]={
  0x10, 0x13, 0x06, 0x10, 0x1F, 0x00, 0x58}; // send request to sensor
int a = 0; // counter
byte dataread[4]; // data byte in reverse order
byte result=0;

void setup(void)
{
  Serial.begin(38400);
  Serial1.begin(38400);
  Serial1.flush();
}
void loop(void)
{
  Serial1.write(myBytes, sizeof(myBytes)); // request data from sensor
  if(Serial1.available()){ 
    while(Serial1.available()){ // receive and collect data
      result=Serial1.read();
      empty[a]=result;
      a++;    
    }    
    a=0;
  }  

  dataread[0] = empty[7];  // extract gas reading from sensor
  dataread[1] = empty[8]; 
  dataread[2] = empty[9]; 
  dataread[3] = empty[10];  

  Serial.print(dataread[0],HEX);
  Serial.print("-");
  Serial.print(dataread[1],HEX);
  Serial.print("-");
  Serial.print(dataread[2],HEX);
  Serial.print("-");
  Serial.print(dataread[3],HEX);
  Serial.print("        ");

  float sens_output = *((float *)dataread); 

  Serial.println(sens_output,7); 

  delay(500); 
}
if(Serial1.available()){ 
    while(Serial1.available()){ // receive and collect data
      result=Serial1.read();
      empty[a]=result;
      a++;    
    }    
    a=0;
  }

Mostly, Serial.available () is only going to return either one or zero - can your code cope with this?

In my case, the Serial1.available(), only is used like a trigger/pointer to tell when bytes come in and to define first byte for array. After sending the request-bytes to the sensor, the program runs in a loop, until ANYTHING comes in on Serial1.

Did I get the question right ?