Split a float in 2 bytes

I have a floating number for example 54.354 and i want to send to over modbus to another device. I wanted to split this number into 2 bytes and send it.
Can anyone help with any function that could split a float into 2 bytes.?

Thanks

Nobody can. A float occupies 4 bytes. You can split it into 4 bytes...

OK so how do i split it in 4 bytes?

ammarqs:
OK so how do i split it in 4 bytes?

a union

union MyUnion{
  uint8_t myByte[4];
  float myfloat;
}

I dont understand. How do i use this?

ammarqs:
I dont understand. How do i use this?

Did you research it at all?

union MyUnion{
  uint8_t myByte[4];
  float myfloat;
}

MyUnion myUnion;

//Somewhere in a function....


myUnion.myfloat = 3.1216;
for (int i = 0; i < 4; i++)
{
  Serial.println(myUnion.myByte[i]);
}

GOOGLE

Did you research it at all?

I am doing it as we speak. Replied to quick on that tip. Sorry about that.

Thank you BulldogLowell

union MyUnion{
uint8_t myByte[4];
float myfloat;
}

MyUnion myUnion;

//Somewhere in a function…

myUnion.myfloat = 3.1216;
for (int i = 0; i < 4; i++)
{
Serial.println(myUnion.myByte*);*
}[/quote]
I tried the above code and it returned:
* *75 200 71 64* *
i tired different floats and it displayed different combinations. I dont understand how does union do its conversaion?

It just reinterprets the memory that holds the float as an array of bytes, there is no conversion.

If you want to learn more about the internal format of floating point numbers have a look at

https://www.h-schmidt.net/FloatConverter/IEEE754.html

I dont understand how does union do its conversaion?

In the UNION, your float variable has 4 bytes of memory reserved.
Those 4 bytes of memory are physically shared with the 4 elements of array myByte.

ammarqs:
I tried the above code and it returned:

75

200
71
64




i tired different floats and it displayed different combinations. I dont understand how does union do its conversaion?

Those are the four bytes your float is made of. You wanted to split a float into 4 bytes and those are the 4 bytes.

I wanted to split this number into 2 bytes and send it.

Were you thinking to send those (4) bytes somewhere in particular, and do something with them once they arrived, or did the thought process simply stall out?

BulldogLowell:
a union

Or a pointer:

void setup() {
  float myFloat;
  float *myFloatPtr;
  Serial.begin(115200);
  delay(2000);

  myFloat = 100.5;
  myFloatPtr = &myFloat;

  // Using pointer dereferencing
  for (uint8_t index=0; index<4; index++) {
    Serial.println(*((uint8_t *)myFloatPtr + index), HEX);
  }

  // Using array notation
  Serial.println();
    for (uint8_t index=0; index<4; index++) {
    Serial.println( ((uint8_t *)myFloatPtr)[index], HEX);
  }
}

void loop() {}