# Transform 4 bytes into a float

**URL:** <https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241>\
**Category:** Sensors\
**Created:** [June 28, 2019, 6:51pm UTC](https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241 "2019-06-28T18:51:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadafaga35](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@Sadafaga35](https://forum.arduino.cc/u/Sadafaga35)\
**Post date:** [June 28, 2019, 6:51pm UTC](https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241/1 "2019-06-28T18:51:58Z")

</div>

Hello

I have 4 bytes that I read from a sensor: byte[0], byte[1], byte[2] and byte[3]. I have to put them in one single float.

I want to put the byte[0] in the first byte of the float, the byte[1] in the second one, the byte[2] in the third one and the byte[3] in the last one.

I believe that operators "\<\<" and "|" could help me but I am not sure and I don't know how to use both of them.

If someone could attach me some code I really appreciate that.

Thanks!

---

<div class="post-metadata">

**Author:** ![zwieblum](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zwieblum/32/427702_2.png) [@zwieblum](https://forum.arduino.cc/u/zwieblum)\
**Post date:** [June 28, 2019, 7:01pm UTC](https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241/2 "2019-06-28T19:01:25Z")

</div>

```cpp
void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
}

union {
 uint8_t b[4];
 float f;
} data;

void loop() {
 // put your main code here, to run repeatedly:
 data.b[0]=0;
 data.b[1]=1;
 data.b[2]=2;
 data.b[3]=3;
 Serial.println(data.f);

}

```

```cpp
​Adjust to your needs.

```

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [June 29, 2019, 4:45am UTC](https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241/3 "2019-06-29T04:45:26Z")

</div>

You can also do this with a [memcpy()](http://www.cplusplus.com/reference/cstring/memcpy/) operation.

```cpp
byte data[4]; // read bytes in this array
float f; // the destination

memcpy(f, data, 4);

```

You can actually even read the bytes directly into the float:

```cpp
float * f;
f[0] = readByte(); // The first byte of data.
f[1] = readByte(); // Second byte of data.
f[2] = readByte(); // etc.
f[3] = readByte();

```

This you'd of course do in a for loop or so.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 29, 2019, 2:28pm UTC](https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241/4 "2019-06-29T14:28:57Z")

</div>

@OP

If you are sure that the discrete 4-byte data that you own are the components of a float number, then I would recommend to choose the advice of Post#1. Assume, float x = 14.53; byte[0] = 0xE1, byte[1] = 0x7A, byte[2] = 0x86, byet[3] = 0x41. (Serial.print(_(long_)&x, HEX); //shows: 41687AE1)

```cpp
data.b[0] = byte[0];
data.b[1] = byte[1];
data.b[2] = byte[2];
data.b[3] = byte[3];

Serial.print(data.f, 2);    //should show: 14.53

```

---

<div class="post-metadata">

**Author:** ![zwieblum](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zwieblum/32/427702_2.png) [@zwieblum](https://forum.arduino.cc/u/zwieblum)\
**Post date:** [June 29, 2019, 2:34pm UTC](https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241/5 "2019-06-29T14:34:43Z")

</div>

> wvmarle:  
> You can actually even read the bytes directly into the float:
> 
> ```arduino
> float * f;
> 
> ```

f[0] = readByte(); // The first byte of data.  
f[1] = readByte(); // Second byte of data.  
f[2] = readByte(); // etc.  
f[3] = readByte();

> ```arduino
> 
> This you'd of course do in a for loop or so.
> 
> ```

Aem, this will give you the most beautiful runtime errors 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 5, 2019, 11:50am UTC](https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241/6 "2019-07-05T11:50:04Z")

</div>

Do not cross-post, duplicate deleted.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 10:26am UTC](https://forum.arduino.cc/t/transform-4-bytes-into-a-float/599241/7 "2021-05-06T10:26:13Z")

</div>

Closed on May 6, 2021, 10:26am UTC


