Go Down

Topic: Split a float in 2 bytes (Read 288 times) previous topic - next topic

ammarqs

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

aarg

Nobody can. A float occupies 4 bytes. You can split it into 4 bytes...
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

ammarqs

OK so how do i split it in 4 bytes?

BulldogLowell

OK so how do i split it in 4 bytes?
a union

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

ammarqs

I dont understand. How do i use this?

aarg

I dont understand. How do i use this?
Did you research it at all?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

BulldogLowell

Code: [Select]
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

ammarqs

Quote
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

ammarqs

Quote
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 tried the above code and it returned:
Code: [Select]
75
200
71
64


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

Whandall

#9
Dec 08, 2017, 01:38 am Last Edit: Dec 08, 2017, 01:42 am by Whandall
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
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

larryd

#10
Dec 08, 2017, 01:44 am Last Edit: Dec 08, 2017, 02:01 am by larryd
Quote
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[].





No technical PMs.
The last thing you did is where you should start looking.

Delta_G

#11
Dec 08, 2017, 01:47 am Last Edit: Dec 08, 2017, 01:48 am by Delta_G
I tried the above code and it returned:
Code: [Select]
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. 
If at first you don't succeed, up - home - sudo - enter.

jremington

#12
Dec 08, 2017, 04:32 am Last Edit: Dec 08, 2017, 04:33 am by jremington
Quote
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?

gfvalvo

a union
Or a pointer:
Code: [Select]
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() {}

Go Up