# how to convert two bytes floating point hexadecimal number to decimal?

Hi there.
I have a sensor which its data format is a frame with this format: begin(2 bytes), command(1 byte), data(2 bytes), parity(1 byte).
The 3 first bytes are fixed. the output should be a decimal floating point number:

I want to convert those two data byte to output, but I can't understand the way to this conversation. Can you help me?

A simple lookup(maybe a little arithmetic) on the first data byte.
Maybe even a switch/case.

My problem is the How to convert that two bytes to a floating point decimal number. Maybe I should use the parity byte too!!!

My reading of the table is that you only need to convert one byte.

Disagree let's see. For example how can I convert DA in hexadecimal to 47.4 in decimal? one byte is less. especially it can sense up to 120 or 130 therefore the only integer part of number needs one byte. Considering the floating part, we need at least two byte.

1. This is the view of your captured picture/data file.

Figure-1:

Which one of the following you want to achieve against 0xD4 and 0x01 (Line-11 of Fig-1)?
Just to see 46.8 on Serial Monitor

or

To store 46.8 in a float variable?

fatemeh_tabrizi:
Disagree let's see. For example how can I convert DA in hexadecimal to 47.4 in decimal? one byte is less. especially it can sense up to 120 or 130 therefore the only integer part of number needs one byte. Considering the floating part, we need at least two byte.

``````if (byteToConvert == 0xda)
{
convertedVal = 47.4;
}
``````

Either I'm underthinking this problem, or you're overthinking it. (Or there is something you're not telling us)

I need to achieve those output. I want to make them, not to memorize them! I need the way to convert DA to 47.4 not to equalization DA to 47.4. OK?

fatemeh_tabrizi:
I need to achieve those output. I want to make them, not to memorize them! I need the way to convert DA to 47.4 not to equalization DA to 47.4. OK?

maybe they us that that mysterious sensor is! with any luck someone may have used is before.... in any case it would help us help YOU!

btw from your capured data you may notice something interesting:

if we look at some of the data, for example

data(HEX) dB value (DEC)
DA 01 47.4
D9 01 47.3
DB 01 47.5

do you see the pattern here?

it seems to me that only the actual dB data is in the first by (+ some conversion arithmetic)

It is not floating point, but fixed point with one decimal.

``````B8 01 → 0x01B8 = 440 → 44.0 dB
CC 01 → 0x01CC = 460 → 46.0 dB
EC 01 → 0x01EC = 492 → 49.2 dB
``````

btw from your capured data you may notice something interesting:

if we look at some of the data, for example

data(HEX) dB value (DEC)
DA 01 47.4
D9 01 47.3
DB 01 47.5

do you see the pattern here?

it seems to me that only the actual dB data is in the first by (+ some conversion arithmetic)

Maybe parity byte needed for converting!!!

Figure-1:

``````byte myData[6] = {0xBB, 0xAA, 0x01, 0xD4, 0x01, 0x3B};  //Line-11 of Fig-1
void setup()
{
Serial.begin(9600);
int x = (myData[4]<<8)| myData[3];  // x = 0x01D4 ==> 468 ==>
float dB = (float) 0.1*x;
Serial.print(dB, 1);     //shows: 46.8
}

void loop()
{

}
``````

oqibidipo:
It is not floating point, but fixed point with one decimal.

``````B8 01 → 0x01B8 = 440 → 44.0 dB
``````

CC 01 → 0x01CC = 460 → 46.0 dB
EC 01 → 0x01EC = 492 → 49.2 dB

nice one oqibidipo!

oqibidipo:
It is not floating point, but fixed point with one decimal.

``````B8 01 → 0x01B8 = 440 → 44.0 dB
``````

CC 01 → 0x01CC = 460 → 46.0 dB
EC 01 → 0x01EC = 492 → 49.2 dB

Excellent, bravo.
Now how should I write this code?

Kudos to oqibidipo! I did not see that.

@fatemeh_tabrizi I would suggest to just store it in an (unsigned) int and keep the fixed point notation. Way nicer than messing with floats.

GolamMostafa:

``````byte myData[6] = {0xBB, 0xAA, 0x01, 0xD4, 0x01, 0x3B};  //Line-11 of Fig-1
``````

void setup()
{
Serial.begin(9600);
int x = (myData[4]<<8)| myData[3];  // x = 0x01D4 ==> 468 ==>
float dB = (float) 0.1*x;
Serial.print(dB, 1);    //shows: 46.8
}

void loop()
{

}

GolamMostafa gave you an example code!

fatemeh_tabrizi:
Excellent, bravo.
Now how should I write this code?

See Post#13.

GolamMostafa:

``````byte myData[6] = {0xBB, 0xAA, 0x01, 0xD4, 0x01, 0x3b};
``````

void setup()
{
Serial.begin(9600);
int x = (myData[4]<<8)| myData[3];  // x = 0x01D4 ==> 468 ==>
float dB = (float) 0.1*x;
Serial.print(dB, 1);     //shows: 46.8
}

void loop()
{

}

Thank you