0

How can I convert a byte array to float?

Code:

void send_data() {
    CCPACKET data;
    data.length=2;

    float lon=26.533255;
    float lat=27.533463;


    data.data[0]=lon;
    data.data[1]=lat;

    if(cc1101.sendData(data)){
    Serial.println(data.data[0]);
    Serial.println(data.data[1]);

    Serial.println(" sent ok ");
    return true;
    }else{
    Serial.println("sent failed ");
    return false;
    }

 }

The problem is when I print those two variables it just prints lon 26.00 and lat 27.00 but not lon 26.533255 lat 27.533463 as I expected.

Here is the header file for CCPACKET I use:

#ifndef _CCPACKET_H
#define _CCPACKET_H

#include "Arduino.h"

/**
 * Buffer and data lengths
 */
#define CC1101_BUFFER_LEN        64
#define CC1101_DATA_LEN          CC1101_BUFFER_LEN - 3

/**
 * Class: CCPACKET
 * 
 * Description:
 * CC1101 data packet class
 */
class CCPACKET
{
  public:
    /**
     * Data length
     */
    byte length;

    /**
     * Data buffer
     */
    byte data[CC1101_DATA_LEN];

    /**
     * CRC OK flag
     */
    boolean crc_ok;

    /**
     * Received Strength Signal Indication
     */
    byte rssi;

    /**
     * Link Quality Index
     */
    byte lqi;
};

#endif

I was trying to fix it by this part of code, but without success:

float lon=26.533543;

*((float *)data.data[0]) = lon;

Can someone help me to convert the byte array to float?

3
  • Do you know how many bytes a float uses? Oct 28, 2014 at 17:02
  • Hmm unfortunately no.
    – AdiT
    Oct 28, 2014 at 17:16
  • 4
    Your first task is to discover the answer. Oct 28, 2014 at 17:36

2 Answers 2

3

You can choose to treat the byte array as a float array by typecasting. Your attempt isn't far from a solution:

((float *)data.data)[0] = lon; // uses data.data[0] ... data.data[3]
((float *)data.data)[1] = lat; // uses data.data[4] ... data.data[7]

When printing you should also treat the data array as float array:

Serial.println(((float *)data.data)[0]);

It looks like you are sending the data over some interface. Then of course you need to typecast the binary data back into a float at the receiving end (if you want to do something useful with it) - similar to what is done above when it is passed to Serial.println.

6
  • Hi thank you for helping me, it was useful :) Now it is possible to send data with decimal. I was trying to receive data like this: loat lon=((float *)packet.data)[0]; float lat=((float *)packet.data)[1]; Serial.println(((float *)packet.data)[0],4); Serial.println(((float *)packet.data)[1],4); But without success. What could the problem be? Again I want to than you :)
    – AdiT
    Oct 29, 2014 at 10:36
  • The output I got is 0.0000 and 0.0000
    – AdiT
    Oct 29, 2014 at 11:18
  • Did you update the packet size (data.length)? Each float eats 4 bytes, so it has to be 8 now.
    – user2973
    Oct 30, 2014 at 6:35
  • Hi, I'm not able to fix it by my self. How/where can I update the the packet size?
    – AdiT
    Oct 30, 2014 at 11:00
  • Try data.length=8 in your send_data routine.
    – user2973
    Oct 30, 2014 at 14:05
0

/** Data buffer/ byte data[CC1101_DATA_LEN];

That line of code declares an array of datatype byte. I suggest you review variable declaration for Arduino.

A byte will store an 8-bit unsigned number with a range of 0 to 255, no decimal points. I also suggest you review truncation to understand how computer systems handle mathematic calculations that produce decimal results when the result is being stored in a variable type that doesn't store decimals.

A float stores a 32-bit value with decimal point.

The quick answer to your problem is to change the above quoted code from byte to float.

2
  • Except now the CC1101 code will need to be changed to accept floats and transmit bytes. So, nothing saved. Oct 28, 2014 at 20:43
  • True, it would have to be followed through with on all related functions. It could get messy. In any case, the issue is that CC library is set up for bytes rather than floats.
    – rjbergen
    Oct 28, 2014 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.