Float to uint8_t

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?

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

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,

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....

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:

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

}myTemp;

myTemp.Fahrenheit = 74.53;

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.
```
*//  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 )
{
  Serial.write( '-' );
  X = -X;
}

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

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

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

Serial.write( '.' );

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

if ( X >= 1 )  // 0.01F in 100ths of 1F
{
  tmp = X / 10;
  Serial.print( tmp );
  X -= tmp * 10;
}
else
{
  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.

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.

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

 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;

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 ?