convert float into binary and hexadecimal value

Hi everyone I am beginner in arduino. I have doubt in floating point conversion. Below I attached my program,

float floatValue=12.15;
void setup(){
Serial.begin(9600);
Serial.print("The given float value is =");
Serial.println(floatValue);
Serial.print("The hex value is =");
Serial.println(floatValue,HEX);
Serial.print("The bin value is =");
Serial.println(floatValue,BIN);
}
void loop(){

}

Output will be like this,

The given float value is =12.15
The hex value is =12.1499996185302734
The bin value is =12.15

I don't know the reason why my hexadecimal and binary value coming like this. At the same time while executing this program by means of int value it will work fine.

Can any one clarify my doubt.

typedef union
{
  float v;
  uint32_t as_int;
}
cracked_float_t;

cracked_float_t floatValue;

void setup()
{
  floatValue.v = 12.15;

  Serial.begin(9600);

  Serial.print("The given float value is =");
  Serial.println(floatValue.v);

  Serial.print("The hex value is =");
  Serial.println(floatValue.as_int,HEX);

   Serial.print("The bin value is =");
  Serial.println(floatValue.as_int,BIN);
}

void loop() {}

It's because if the first parameter to .print() is a float, the second parameter is NOT the base of the number but the amount of decimal places :wink:

So using a union should work. But also, just casting it to a long should work.

septillion:
But also, just casting it to a long should work.

That would just convert the float to an integer, which is not what you want.
You could, however, try something like this:

[color=#000000]static_assert[/color][color=#000000]([/color][color=#00979c]sizeof[/color][color=#000000]([/color][color=#00979c]float[/color][color=#000000])[/color] [color=#434f54]==[/color] [color=#00979c]sizeof[/color][color=#000000]([/color][color=#00979c]uint32_t[/color][color=#000000])[/color][color=#434f54],[/color] [color=#005c5f]"Error: invalid float size"[/color][color=#000000])[/color][color=#000000];[/color]
[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]115200[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#00979c]float[/color] [color=#000000]f[/color] [color=#434f54]=[/color] [color=#00979c]PI[/color][color=#000000];[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#434f54]*[/color][color=#000000]([/color][color=#00979c]uint32_t[/color][color=#434f54]*[/color][color=#000000])[/color][color=#434f54]&[/color][color=#000000]f[/color][color=#434f54],[/color] [color=#00979c]HEX[/color][color=#000000])[/color][color=#000000];[/color] [color=#434f54]// Prints '40490FDB'[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#000000]([/color][color=#00979c]uint32_t[/color][color=#000000])[/color][color=#000000]f[/color][color=#434f54],[/color] [color=#00979c]HEX[/color][color=#000000])[/color][color=#000000];[/color]    [color=#434f54]// Prints '3'[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color] [color=#000000]}[/color]

Pieter

*(uint32_t*)&f

I believe such things are increasingly likely to run into aliasing trouble (undefined behaviour, no longer legal). A reinterpret_cast may save the day (either by working correctly or flagging the trouble).

Thank you for your quick response. I will check this out.

I have doubt in this line

Serial.println(floatValue.as_int,HEX);

How to store this converted hex value.

Ganesh2807:
I have doubt in this line

Serial.println(floatValue.as_int,HEX);

How to store this converted hex value.

What are you trying to do?

I want to convert float into byte and back again. With the help of this HEX value I can this byte into float again. So that only I ask.

You store it as a float. The conversion to hex only happens during printing.

You store it as a float. The conversion to hex only happens during printing.

Yeah exactly.

I want to convert float into byte and back again.

Use a union, similar to reply #1. Works either way

typedef union
{
  float v;
  uint8_t b[4];
}
cracked_float_t;

cracked_float_t floatValue;

void setup()
{
  floatValue.v = 12.15;

  Serial.begin(9600);

  Serial.print("The given float value is =");
  Serial.println(floatValue.v);

  Serial.print("The binary values are =");
  for (int i = 0; i < 4; i++) {

    Serial.print(" ");
    Serial.print(floatValue.b[i]);

  }
  Serial.println();

  floatValue.b[2]=255;

  Serial.print("The modified float value is =");
  Serial.println(floatValue.v);

}

void loop() {}

jremington:
Use a union, similar to reply #1. Works either way

Or both ways...

typedef union
{
  float v;
  uint8_t b[sizeof(float)];
  uint32_t as_int;
}
cracked_float_t;

You can have exact bit pattern for 12.25 if you ask for binary32 format. If you wish, you can use paper -pencil method to get binary value of 12.15 (1100.001000111111...).

Thank you guys for guiding me. I have small doubt in this below program,

typedef union{
float v;
int16_t loWord,hiWord;
byte b[sizeof(float)];
}
cracked_float_t;
cracked_float_t floatValue;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
floatValue.v=263.3;
Serial.print("The float value is =");
Serial.println(floatValue.v);
Serial.print("The byte value of given float is =");
for(int i=0;i<4;i++){
Serial.print(floatValue.b*,HEX);*

  • Serial.println(" ");*
    }
    floatValue.loWord=word(floatValue.b[1],floatValue.b[0]);
    floatValue.hiWord=word(floatValue.b[3],floatValue.b[2]);
    Serial.print("The 16 bit low byte is =");
    Serial.println(floatValue.loWord,HEX);
    Serial.print("THE 16 bit high byte is =");
    Serial.println(floatValue.hiWord,HEX);
    }
    void loop() {
  • // put your main code here, to run repeatedly:*
    }
    OUTPUT is,
    The float value is =263.30
    The byte value of given float is =66
    A6
    83
    43
    The 16 bit low byte is =4383
    THE 16 bit high byte is =4383
    My Excepted Output is,
    The float value is =263.30
    The byte value of given float is =66
    A6
    83
    43
    The 16 bit low byte is =4383
    THE 16 bit high byte is =A666
    I want a output will be 16 bit high byte = A666 but I get a output as 16 bit high byte = 4383.Can anyone providing a solution for this problem.

Your declaration...

typedef union{
  float v;
  int16_t loWord,hiWord;
  byte b[sizeof(float)];
}

...can be rewritten as...

typedef union{
  float v;
  int16_t loWord;
  int16_t hiWord;
  byte b[sizeof(float)];
}

See the problem?

Do you now understand why b is an array?

OUTPUT is,
The float value is =263.30
The byte value of given float is =66
A6
83
43
The 16 bit low byte is =4383
THE 16 bit high byte is =4383

My Excepted Output is,

The float value is =263.30
The byte value of given float is =66
A6
83
43
The 16 bit low byte is =4383
THE 16 bit high byte is =A666

My Excepted Output is,

The float value is =263.30
The byte value of given float is =66
A6
83
43
The 16 bit low byte is =A666
THE 16 bit high byte is =4383
My excepted output will be 16 bit low byte = A666 but I get a output as 16 bit low byte = 4383.Can anyone providing a solution for this problem.

Is that a "no" to my two questions?

Sorry to say this. I made a small mistake in the excepted output line. So that only I post again.

Have a look at reply #15 again. And note, it's a union. EVERY definition inside it has the same memory address. Even loWord and hiWord. Do you still expect them to differ?