Convert Float To 4 Bytes And Then Back Again - Mbed

  • Overview

    Operating system

    • Mbed OS

      The open source OS for Cortex-M devices

    Development tools

    • Keil Studio Cloud

      Arm's IDE for IoT, embedded and Mbed development

    • Mbed Studio

      Download the desktop IDE for Mbed OS

    • Mbed CLI

      Command line access to Mbed tools and services

    Security and Connectivity

    • Mbed TLS and Mbed Crypto

      Industry standard TLS stack and crypto library

    • Connectivity

      BLE, WiFi, Cellular, LoRaWAN and more

  • Hardware
    • Boards

      Build your Mbed projects with development boards for Arm Cortex processors and MCUs

    • Modules

      Modules include a MCU, connectivity and onboard memory, making them ideal for designing IoT products for mass production

    • Components

      The component database hosts libraries for different sensors, actuators, radios, inputs, middleware and IoT services

    • Hardware overview & Mbed Enabled

      Learn about hardware support for Mbed, as well as the Mbed Enabled program, which identifies Mbed compatible products

    • Mbed HDK

      Reference designs, schematics and board layouts to develop production hardware and Mbed-compatible development boards

  • Code
  • Documentation
    • Mbed OS
    • Keil Studio
    • Mbed Studio
  • Case studies
  • Community
    • Forum
    • Contribute
    • Mbed OS Tech Forum
    • Report a problem
  • Blog
Keil Studio Cloud
    • Log in or Sign up
Log in or Sign up
  • Overview
    • Operating system

      • Mbed OS

    • Development tools

      • Mbed Studio

      • Keil Studio

      • Mbed CLI

    • Security and connectivity

      • Mbed TLS and Mbed Crypto

      • Connectivity

  • Hardware
    • Boards

    • Modules

    • Components

    • Hardware overview & Mbed Enabled

    • Mbed HDK

  • Code
  • Documentation
    • Mbed OS
    • Mbed Studio
    • Keil Studio
  • Case studies
  • Community
    • Forum
    • Contribute
    • Mbed OS Tech Forum
    • Report a problem
  • Blog

Important update: Arm Announces End of Life Timeline for Mbed. This site will be archived in July 2026. Read the full announcement.

Forum » Hello World! » Convert float to 4 Bytes and then back again

Important changes to forums and questions

All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.

Convert float to 4 Bytes and then back again

Topic last updated 17 Apr 2019, by Ayoub Ayoub. 17 replies Tom Gregory tomgregory # 24 Mar 2011

I need to convert a float to 4 bytes so it can be sent on a 8 bit data channel. I then need to convert these 4 bytes back to a float. Can anyone advice me on how to do this.

Thanks

I need to convert a float to 4 bytes so it can be sent on a 8 bit data channel. I then need to convert these 4 bytes back to a float. Can anyone advice me on how to do this. Thanks Igor Skochinsky igorsk # 24 Mar 2011

The simplest way is just to treat it as a buffer.

float x; i2c.write(addr, (char*)&x, sizeof(x)); ... i2c.read(addr, (char*)&x, sizeof(x));

If you need to access each byte separately, use a union:

union float2bytes { float f; char b[sizeof(float)]; }; float2bytes f2b; float x; f2b.f = x; for ( int i=0; i < sizeof(float); i++ ) send_byte(f2b.b[i]); ... for ( int i=0; i < sizeof(float); i++ ) f2b.b[i] = read_byte(); x = f2b.f;

Note that you need to make sure the sender and receiver both use the same endianness and floating-point format.

The simplest way is just to treat it as a buffer. <<code>> float x; i2c.write(addr, (char*)&x, sizeof(x)); ... i2c.read(addr, (char*)&x, sizeof(x)); <</code>> If you need to access each byte separately, use a union: <<code>> union float2bytes { float f; char b[sizeof(float)]; }; float2bytes f2b; float x; f2b.f = x; for ( int i=0; i < sizeof(float); i++ ) send_byte(f2b.b[i]); ... for ( int i=0; i < sizeof(float); i++ ) f2b.b[i] = read_byte(); x = f2b.f; <</code>> Note that you need to make sure the sender and receiver both use the same endianness and floating-point format. Andy K AjK # 24 Mar 2011

Something like this should work. Use Google to find out more about C unions. You''ll also want to make sure you know how much C data types take up space wise.

typedef union _data { float f; char s[4]; } myData; #include "mbed.h" Serial pc(USBTX, USBRX); myData q; int main() { q.f = 1.234; pc.printf("%c%c%c%c", q.s[0], q.s[1], q.s[2], q.s[3]); }

I'l leave you to work out the reverse from chars back to float :)

Something like this should work. Use Google to find out more about C unions. You''ll also want to make sure you know how much [[http://mbed.org/handbook/C-Data-Types|C data types]] take up space wise. <<code>> typedef union _data { float f; char s[4]; } myData; #include "mbed.h" Serial pc(USBTX, USBRX); myData q; int main() { q.f = 1.234; pc.printf("%c%c%c%c", q.s[0], q.s[1], q.s[2], q.s[3]); } <</code>> I'l leave you to work out the reverse from chars back to float :) Yoko Hama hoan # 25 Mar 2011

simplest way

float f; uint8_t *p = (uint8_t*)&f;

for (int i = 0; i < sizeof(f); i++) printf("0x%02x ", p[i]);

simplest way float f; uint8_t *p = (uint8_t*)&f; for (int i = 0; i < sizeof(f); i++) printf("0x%02x ", p[i]); Tom Gregory tomgregory # 26 Mar 2011

Thanks for your help.

Thanks for your help. Luis Linares macgyveremir # 18 Oct 2011

My solution is to employ typecasts. This makes it, of course, endianness dependent, which as explained in here, is little-endian for the LPC1768. For the examples, let's assume the float starts at position 3 of the byte array. so:

char my_array[10]; float my_float; my_float = *( (float*)(my_array + 3) ); // from array to float

From float to array, any of the above solutions works as well :)

My solution is to employ typecasts. This makes it, of course, endianness dependent, which as explained in [[http://mbed.org/forum/mbed/topic/2195/?page=1#comment-11097|here]], is little-endian for the LPC1768. For the examples, let's assume the float starts at position 3 of the byte array. so: <<code>> char my_array[10]; float my_float; my_float = *( (float*)(my_array + 3) ); // from array to float <</code>> From float to array, any of the above solutions works as well :) steve childress stevech # 18 Oct 2011

In C, the union type is often used. Something like

union floatUnion_t { float f; char bytes[sizeof float] } floatUnion1; In C, the union type is often used. Something like <<code>> union floatUnion_t { float f; char bytes[sizeof float] } floatUnion1; <</code>> Javier Sing javiersing # 06 Dec 2016

Andy K wrote:

Something like this should work. Use Google to find out more about C unions. You''ll also want to make sure you know how much C data types take up space wise.

typedef union _data { float f; char s[4]; } myData; #include "mbed.h" Serial pc(USBTX, USBRX); myData q; int main() { q.f = 1.234; pc.printf("%c%c%c%c", q.s[0], q.s[1], q.s[2], q.s[3]); }

I'l leave you to work out the reverse from chars back to float :)

Hi, im trying to convert a float variable in to hex and store it in a new variable, will this work for doing this?

<<quote AjK>> Something like this should work. Use Google to find out more about C unions. You''ll also want to make sure you know how much [[http://mbed.org/handbook/C-Data-Types|C data types]] take up space wise. <<code>> typedef union _data { float f; char s[4]; } myData; #include "mbed.h" Serial pc(USBTX, USBRX); myData q; int main() { q.f = 1.234; pc.printf("%c%c%c%c", q.s[0], q.s[1], q.s[2], q.s[3]); } <</code>> I'l leave you to work out the reverse from chars back to float :) <</quote>> Hi, im trying to convert a float variable in to hex and store it in a new variable, will this work for doing this? melvin awesome melvinyong # 21 Jul 2017

Troubleshooting!

Hi guys i need some help with my code. i trying to convert readings in byte array to float with union but can't seem to work. i manage to get reading of the float but the reading is 0.00 Here is my code:

main.cpp

#include "mbed.h" typedef union _data { float f; char buffer2[4]; } myData; myData q; //-------------------------------------- //| Hyperterminal configuration | //| 115200 bauds, 8-bit data, no parity | //-------------------------------------- Serial pc(SERIAL_TX, SERIAL_RX); //PC to STM32 Board //Serial transmit(PC_4,PC_5); // STM32 writing/reading with Arduino Serial uart(PC_4,PC_5); //DigitalOut myled(LED1); DigitalIn BlueButton(USER_BUTTON); // This is Blue-Button and is on NUCLEO-L153RE DigitalOut pc_activity(LED1); DigitalOut uart_activity(LED2); #define Pressed 0 #define NotPressed 1 int main() { char buffer[5]; char buffer1[4]; //float my_float; pc.printf("Select one of the following..."); pc.printf("\r\n"); pc.printf("i = deice type and version number"); pc.printf("\r\n"); pc.printf("r = take a single pH reading"); pc.printf("\r\n"); uart.baud(115200); //Setting of baud rate int printready =0; int i =0; while(1) { if(pc.readable()) { //If PC port got reading/get input of char uart.putc(pc.getc()); //wait(0.5); //uart.clear(); //Sending char to arduino pc.printf("Message Sent!\r\n"); //pc_activity = !pc_activity; //LED indication while(uart.readable()) { //UART excute only when receive reading char tempc = uart.getc(); // if(tempc != 'v') buffer[i] = tempc;//uart.getc(); wait(0.5); i++; } printready = 1; } //sending working if(printready == 1){ pc.printf("uart is %s",buffer); for(int j = 0; j< 4; j++) buffer1[j] = buffer[j+1]; pc.printf("\nnew buffer is %s",buffer1); pc.printf("\r\n"); //my_float = *( (float*)(buffer1 + 3) ); //not working // q.buffer2[0]=buffer1[1]; // q.buffer2[1]=buffer1[2]; // q.buffer2[2]=buffer1[3]; // q.buffer2[3]=buffer1[4]; pc.printf("Float:%.2f", q.f ); printready = 0; } } } <<warning title="Troubleshooting!">> Hi guys i need some help with my code. i trying to convert readings in byte array to float with union but can't seem to work. i manage to get reading of the float but the reading is 0.00 Here is my code: <<code main.cpp>> #include "mbed.h" typedef union _data { float f; char buffer2[4]; } myData; myData q; //-------------------------------------- //| Hyperterminal configuration | //| 115200 bauds, 8-bit data, no parity | //-------------------------------------- Serial pc(SERIAL_TX, SERIAL_RX); //PC to STM32 Board //Serial transmit(PC_4,PC_5); // STM32 writing/reading with Arduino Serial uart(PC_4,PC_5); //DigitalOut myled(LED1); DigitalIn BlueButton(USER_BUTTON); // This is Blue-Button and is on NUCLEO-L153RE DigitalOut pc_activity(LED1); DigitalOut uart_activity(LED2); #define Pressed 0 #define NotPressed 1 int main() { char buffer[5]; char buffer1[4]; //float my_float; pc.printf("Select one of the following..."); pc.printf("\r\n"); pc.printf("i = deice type and version number"); pc.printf("\r\n"); pc.printf("r = take a single pH reading"); pc.printf("\r\n"); uart.baud(115200); //Setting of baud rate int printready =0; int i =0; while(1) { if(pc.readable()) { //If PC port got reading/get input of char uart.putc(pc.getc()); //wait(0.5); //uart.clear(); //Sending char to arduino pc.printf("Message Sent!\r\n"); //pc_activity = !pc_activity; //LED indication while(uart.readable()) { //UART excute only when receive reading char tempc = uart.getc(); // if(tempc != 'v') buffer[i] = tempc;//uart.getc(); wait(0.5); i++; } printready = 1; } //sending working if(printready == 1){ pc.printf("uart is %s",buffer); for(int j = 0; j< 4; j++) buffer1[j] = buffer[j+1]; pc.printf("\nnew buffer is %s",buffer1); pc.printf("\r\n"); //my_float = *( (float*)(buffer1 + 3) ); //not working // q.buffer2[0]=buffer1[1]; // q.buffer2[1]=buffer1[2]; // q.buffer2[2]=buffer1[3]; // q.buffer2[3]=buffer1[4]; pc.printf("Float:%.2f", q.f ); printready = 0; } } } <</code>> <</warning>> randy ransier oliver27b # 18 Jan 2018

some old functions i had laying around if anyone wants to get into bit bangin stuff you can split and or rejoin 32bits and 16bits to and from 8bits just ignore the printf's they are for testing on a local compiler, the math is in the shifts (32 bit are down a little)

//// // splits a 16bit into 2 8bits rightshifty16 (int16_t argn) { unsigned char bytes[2]; bytes[0] = (argn >> 8) & 0xFF; bytes[1] = argn & 0xFF; printf("0x%02x 0x%02x\n", (unsigned char)bytes[0], (unsigned char)bytes[1]); } //// // combines 2 8bits to a 16bit leftshifty16 (unsigned char argb[]) { int16_t integers; integers = (argb[0] <<8) + argb[1]; printf("%d\n", (int16_t) integers); } //// // splits a 32bit into 4 8bits rightshifty32 (int16_t argn) { unsigned char bytes[4]; bytes[0] = (argn >> 24) & 0xFF; bytes[1] = (argn >> 16) & 0xFF; bytes[2] = (argn >> 8) & 0xFF; bytes[3] = argn & 0xFF; printf("0x%x 0x%x 0x%x 0x%x\n", (unsigned char)bytes[0], (unsigned char)bytes[1], (unsigned char)bytes[2], (unsigned char)bytes[3]); } //// // combines 4 8bits to a 32bit leftshifty32 (unsigned char argb[]) { int32_t integers; integers = (argb[0] <<24) + (argb[1] <<16) + (argb[2] <<8) + argb[3]; printf("%d\n", (int32_t) integers); printf("0x%x\n", (int32_t) integers); } some old functions i had laying around if anyone wants to get into bit bangin stuff you can split and or rejoin 32bits and 16bits to and from 8bits just ignore the printf's they are for testing on a local compiler, the math is in the shifts (32 bit are down a little) <<code>> //// // splits a 16bit into 2 8bits rightshifty16 (int16_t argn) { unsigned char bytes[2]; bytes[0] = (argn >> 8) & 0xFF; bytes[1] = argn & 0xFF; printf("0x%02x 0x%02x\n", (unsigned char)bytes[0], (unsigned char)bytes[1]); } //// // combines 2 8bits to a 16bit leftshifty16 (unsigned char argb[]) { int16_t integers; integers = (argb[0] <<8) + argb[1]; printf("%d\n", (int16_t) integers); } //// // splits a 32bit into 4 8bits rightshifty32 (int16_t argn) { unsigned char bytes[4]; bytes[0] = (argn >> 24) & 0xFF; bytes[1] = (argn >> 16) & 0xFF; bytes[2] = (argn >> 8) & 0xFF; bytes[3] = argn & 0xFF; printf("0x%x 0x%x 0x%x 0x%x\n", (unsigned char)bytes[0], (unsigned char)bytes[1], (unsigned char)bytes[2], (unsigned char)bytes[3]); } //// // combines 4 8bits to a 32bit leftshifty32 (unsigned char argb[]) { int32_t integers; integers = (argb[0] <<24) + (argb[1] <<16) + (argb[2] <<8) + argb[3]; printf("%d\n", (int32_t) integers); printf("0x%x\n", (int32_t) integers); } <</code>> Steve Mylroie Roietronics # 14 Feb 2018

Using union for byte to int32 works on x86 architecture processor but not on ARM based processor since the ARM architectures do not support sub word addressing A byte on ARM occupies a full word in memory on a ARM processor

Using union for byte to int32 works on x86 architecture processor but not on ARM based processor since the ARM architectures do not support sub word addressing A byte on ARM occupies a full word in memory on a ARM processor Zoltan Hudak hudakz # 15 Feb 2018

It seems that union works also on ARM processors. Check the code below:

#include "mbed.h" union Float { float m_float; uint8_t m_bytes[sizeof(float)]; }; float pi; uint8_t bytes[sizeof(float)]; Float myFloat; int main() { pi = 3.141565f; printf("\r\n*******************************************\r\n"); printf("*******************************************\r\n\r\n"); printf("pi = %.6f\r\n", pi); printf("bytes = [ %d, %d, %d, %d]\r\n", bytes[0], bytes[1], bytes[2], bytes[3]); printf("\r\n***** Conversion by using type casting *****\r\n\r\n"); *(float*)(bytes) = pi; // convert float to bytes printf("bytes = [ 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x ]\r\n", bytes[0], bytes[1], bytes[2], bytes[3]); // send bytes over data channel .. // receive bytes .. pi = *(float*)(bytes); // convert bytes back to float printf("pi = %.6f\r\n", pi); printf("\r\n********* Conversion by using union *********\r\n\r\n"); myFloat.m_float = pi; // assign a float to union printf("myFloat.m_Float = %.6f\r\n", myFloat.m_float); printf("myFloat.m_Bytes = [ 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x ]\r\n", myFloat.m_bytes[0], myFloat.m_bytes[1], myFloat.m_bytes[2], myFloat.m_bytes[3]); // get the bytes // send bytes over the data channel .. // receive bytes and assign them to the union bytes: myFloat.m_bytes[0] = received_bytes[0] .. pi = myFloat.m_float; // get the float back from the union printf("pi = %.6f\r\n", pi); } It seems that union works also on ARM processors. Check the code below: <<code>> #include "mbed.h" union Float { float m_float; uint8_t m_bytes[sizeof(float)]; }; float pi; uint8_t bytes[sizeof(float)]; Float myFloat; int main() { pi = 3.141565f; printf("\r\n*******************************************\r\n"); printf("*******************************************\r\n\r\n"); printf("pi = %.6f\r\n", pi); printf("bytes = [ %d, %d, %d, %d]\r\n", bytes[0], bytes[1], bytes[2], bytes[3]); printf("\r\n***** Conversion by using type casting *****\r\n\r\n"); *(float*)(bytes) = pi; // convert float to bytes printf("bytes = [ 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x ]\r\n", bytes[0], bytes[1], bytes[2], bytes[3]); // send bytes over data channel .. // receive bytes .. pi = *(float*)(bytes); // convert bytes back to float printf("pi = %.6f\r\n", pi); printf("\r\n********* Conversion by using union *********\r\n\r\n"); myFloat.m_float = pi; // assign a float to union printf("myFloat.m_Float = %.6f\r\n", myFloat.m_float); printf("myFloat.m_Bytes = [ 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x ]\r\n", myFloat.m_bytes[0], myFloat.m_bytes[1], myFloat.m_bytes[2], myFloat.m_bytes[3]); // get the bytes // send bytes over the data channel .. // receive bytes and assign them to the union bytes: myFloat.m_bytes[0] = received_bytes[0] .. pi = myFloat.m_float; // get the float back from the union printf("pi = %.6f\r\n", pi); } <</code>> Ayoub Ayoub Ayoub_daas # 17 Apr 2019

It works

Zoltan Hudak wrote:

It seems that union works also on ARM processors. Check the code below:

#include "mbed.h" union Float { float m_float; uint8_t m_bytes[sizeof(float)]; }; float pi; uint8_t bytes[sizeof(float)]; Float myFloat; int main() { pi = 3.141565f; printf("\r\n*******************************************\r\n"); printf("*******************************************\r\n\r\n"); printf("pi = %.6f\r\n", pi); printf("bytes = [ %d, %d, %d, %d]\r\n", bytes[0], bytes[1], bytes[2], bytes[3]); printf("\r\n***** Conversion by using type casting *****\r\n\r\n"); *(float*)(bytes) = pi; // convert float to bytes printf("bytes = [ 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x ]\r\n", bytes[0], bytes[1], bytes[2], bytes[3]); // send bytes over data channel .. // receive bytes .. pi = *(float*)(bytes); // convert bytes back to float printf("pi = %.6f\r\n", pi); printf("\r\n********* Conversion by using union *********\r\n\r\n"); myFloat.m_float = pi; // assign a float to union printf("myFloat.m_Float = %.6f\r\n", myFloat.m_float); printf("myFloat.m_Bytes = [ 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x ]\r\n", myFloat.m_bytes[0], myFloat.m_bytes[1], myFloat.m_bytes[2], myFloat.m_bytes[3]); // get the bytes // send bytes over the data channel .. // receive bytes and assign them to the union bytes: myFloat.m_bytes[0] = received_bytes[0] .. pi = myFloat.m_float; // get the float back from the union printf("pi = %.6f\r\n", pi); }

It works <<quote hudakz>> It seems that union works also on ARM processors. Check the code below: <<code>> #include "mbed.h" union Float { float m_float; uint8_t m_bytes[sizeof(float)]; }; float pi; uint8_t bytes[sizeof(float)]; Float myFloat; int main() { pi = 3.141565f; printf("\r\n*******************************************\r\n"); printf("*******************************************\r\n\r\n"); printf("pi = %.6f\r\n", pi); printf("bytes = [ %d, %d, %d, %d]\r\n", bytes[0], bytes[1], bytes[2], bytes[3]); printf("\r\n***** Conversion by using type casting *****\r\n\r\n"); *(float*)(bytes) = pi; // convert float to bytes printf("bytes = [ 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x ]\r\n", bytes[0], bytes[1], bytes[2], bytes[3]); // send bytes over data channel .. // receive bytes .. pi = *(float*)(bytes); // convert bytes back to float printf("pi = %.6f\r\n", pi); printf("\r\n********* Conversion by using union *********\r\n\r\n"); myFloat.m_float = pi; // assign a float to union printf("myFloat.m_Float = %.6f\r\n", myFloat.m_float); printf("myFloat.m_Bytes = [ 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x ]\r\n", myFloat.m_bytes[0], myFloat.m_bytes[1], myFloat.m_bytes[2], myFloat.m_bytes[3]); // get the bytes // send bytes over the data channel .. // receive bytes and assign them to the union bytes: myFloat.m_bytes[0] = received_bytes[0] .. pi = myFloat.m_float; // get the float back from the union printf("pi = %.6f\r\n", pi); } <</code>> <</quote>>
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.

Accept and hide this message

Access Warning

You do not have the correct permissions to perform this operation.

×

Tag » Arduino Convert 4 Bytes To Int