# Float to uint8\_t

**URL:** <https://forum.arduino.cc/t/float-to-uint8_t/634399>\
**Category:** Programming\
**Created:** [February 1, 2020, 10:04pm UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399 "2020-02-01T22:04:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlos14](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@carlos14](https://forum.arduino.cc/u/carlos14)\
**Post date:** [February 1, 2020, 10:04pm UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/1 "2020-02-01T22:04:17Z")

</div>

```cpp
I have two variables

uint8_t charData[6];
float Fahrenheit = 0;

I need to convert the float value to an uint8_t type. The float value comes from a temp sensor, DS18B20, for example, 74.53

sensors.requestTemperatures();
Celcius = sensors.getTempCByIndex(0);
Fahrenheit = sensors.toFahrenheit(Celcius);

Elsewhere in the code, to convert the float value to uint8_t, among many other things I tried:

dtostrf(Fahrenheit, 0, 5, charData);

also:

String sensorData = String(Fahrenheit);
sensorData.toCharArray(charData, sensorData.length() + 1);

One may think that doing this should be very simple, but I have not been able to figure it out. Can anyone help, please?

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 1, 2020, 10:13pm UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/2 "2020-02-01T22:13:21Z")

</div>

What you expect the value 74.53 to be assuming that you manage to convert it into a byte ?

---

<div class="post-metadata">

**Author:** ![carlos14](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@carlos14](https://forum.arduino.cc/u/carlos14)\
**Post date:** [February 2, 2020, 3:25am UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/3 "2020-02-02T03:25:33Z")

</div>

```cpp
I am sorry, I don't understand the question. However, what I am trying to do is to send the value to the Adafruit IO platform. I included Adafruit's Adafruit_MQTT and Adafruit_MQTT_Client libraries.

I did the setup following the sample code:

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish publishData = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/myFirstValue");

and later in the code I am trying to publish the value:

if (! publishData.publish(charData)) {
  Serial.println(F("Failed"));
} else {
  Serial.println(F("OK"));
}

I hope my explanation makes sense.

Thank you,

```

---

<div class="post-metadata">

**Author:** ![DrAzzy](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@DrAzzy](https://forum.arduino.cc/u/DrAzzy)\
**Post date:** [February 2, 2020, 3:56am UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/4 "2020-02-02T03:56:04Z")

</div>

a uint8\_t is a byte - a single value between 0 and 255.

It sounds like you are trying to convert it to a c string (array of characters) - which is an array of characters (int8\_t).

This all suggests that there is some confusion on your part about what you have to do. What makes you think it should be a uint8\_t? Is there some error message that is suggesting that, or what? A quick look at that library shows that there is no publish() method that takes a uint8\_t array like you are attempting to pass it (though you haven't told us what version you are using - popular libraries often have many versions on various peoples' github with significant differences, so you need to specify where you got it from). In fact, the version I am looking at has a publish() method that takes a floating point number....

---

<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:** [February 2, 2020, 3:58am UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/5 "2020-02-02T03:58:12Z")

</div>

**union** data structure could be used to convert a float number into 32-bit (4-byte) binary32/IEEE-754 formatted data and vice versa. For example:

```cpp
union
{
    float Fahrenheit;
    uint8_t myData[4];     //low order member holds low order byte (little endianness) 

}myTemp;

myTemp.Fahrenheit = 74.53;

```

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [February 2, 2020, 5:18am UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/6 "2020-02-02T05:18:16Z")

</div>

> carlos14:  
> I have two variables
> 
> uint8\_t charData[6];  
> float Fahrenheit = 0;
> 
> I need to convert the float value to an uint8\_t type.

Just for starts; code tags are for code, not the whole post.

Your variables tell me you want convert float to text as uint8\_t (also named byte). Why not type char, int8\_t, since C libraries functions use type char for strings?

Since you know the max temperature will be less than 1000,

char number[10]; // float may be 7 places plus decimal point plus sign if negative and terminating zero.

* * *

_But really the only time you convert variable to text is to print to screen or file._  
_You don't -need- the string at all, it is a waste of RAM since there are numeric ways to pull sign and each digit out of that float._  
_-----------------------------------_  
_I'd start by loading 100 x Farenheit into a long which rounds off past 2 places and ends slow sand-and-dirt floating point math._  
_```_  
\*//&nbsp; untested, never compiled but the logic is old hat to me

long X, tmp;

// --- and inside of a printTemperature function

X = (long)( Farenheit \* 100.0);

if ( X \< 0 )  
{  
&nbsp; Serial.write( '-' );  
&nbsp; X = -X;  
}

if ( X \>= 10000 )&nbsp; // 100F in 100ths of 1F  
{  
&nbsp; tmp = X / 10000;  
&nbsp; Serial.print( tmp );  
&nbsp; X -= tmp \* 10000;  
}

if ( X \>= 1000 )&nbsp; // 10F in 100ths of 1F  
{  
&nbsp; tmp = X / 1000;  
&nbsp; Serial.print( tmp );  
&nbsp; X -= tmp \* 1000;  
}

if ( X \>= 100 )&nbsp; // 1F in 100ths of 1F  
{  
&nbsp; tmp = X / 100;  
&nbsp; Serial.print( tmp );  
&nbsp; X -= tmp \* 100;  
}  
else  
{  
&nbsp; Serial.write( '0' );  
}

Serial.write( '.' );

if ( X \>= 10 )&nbsp; // 0.1F in 100ths of 1F  
{  
&nbsp; tmp = X / 10;  
&nbsp; Serial.print( tmp );  
&nbsp; X -= tmp \* 10;  
}  
else  
{  
&nbsp; Serial.write( '0' );  
}

if ( X \>= 1 )&nbsp; // 0.01F in 100ths of 1F  
{  
&nbsp; tmp = X / 10;  
&nbsp; Serial.print( tmp );  
&nbsp; X -= tmp \* 10;  
}  
else  
{  
&nbsp; Serial.write( '0' );  
}\*  
_```_  
_It could be shortened with a little trouble but it adds a lot less to the code size than sprintf() and uses very little RAM._  
_It might even run quicker... there's a fast divide by 10 routine in the Arduino Playground that'd make that pretty certain._

---

<div class="post-metadata">

**Author:** ![carlos14](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@carlos14](https://forum.arduino.cc/u/carlos14)\
**Post date:** [February 2, 2020, 8:59pm UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/7 "2020-02-02T20:59:07Z")

</div>

As @DrAzzy suggested, I am trying to convert from float to int8\_t. As you may have noticed, I am not very proficient in C or C++. I still don't know how to do the conversion.

Thank you @GoForSmoke for the clarification about code tags.

Thank you everybody else for your comments.

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [February 2, 2020, 9:23pm UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/8 "2020-02-02T21:23:23Z")

</div>

here is a snippet that I use to break down a 32 bit number into 4 bytes (unit8\_t)

```cpp
 rx_frame.data.u8[0] = *item & 0xFF;
          rx_frame.data.u8[1] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[2] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[3] = (*item >> 24) & 0xFF;

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 2, 2020, 9:32pm UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/9 "2020-02-02T21:32:40Z")

</div>

> As @DrAzzy suggested, I am trying to convert from float to int8\_t

> I still don't know how to do the conversion.

Let's try to find out what you really want to do  
Given a float, say 1234.56, what do you expect it to look like when converted to uint8\_t and how many bytes will be required to hold the result ?

---

<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, 12:17am UTC](https://forum.arduino.cc/t/float-to-uint8_t/634399/10 "2021-05-06T00:17:25Z")

</div>

Closed on May 6, 2021, 12:17am UTC


