Convert a Hex value to measurement value

Hello ,

I get a HEX value from a measurement ,

-130 A
-120 12
-100 25
-90 2D
-80 37
-70 41
-60 4D
-50 57
-40 5F
-30 4B

How can i calculate the HEX value to the right number ?

Where is this measurement coming from and how are you printing it ?

Please post a complete sketch that shows what you are doing

not sure about that last value (-30, 0x4b)

so 10, 0xA, corresponds to -130 an 95, 0x5f, corresponds to -40. that's a slope of 1.06, (-40 - -130) / (95 - 10)

can you find the intercept?

please show us the code

It is Serial coming in as a HEX byte , i want to know how to calculate the HEX back to the decimal value it represents .

all processors values are binary. it's simply how they are displayed.

Are those values of post #1 comma (,) separated and are members of an array?

There are no hex bytes. For a computer, all bytes exist only in binary form.
Hex is a representation for a human, i.e. you. You yourself choose in what form to print bytes into the output.

That's why I ask you to show you the code, how you output it to the Serial.

Sorry for the confusion,

I get the value from another device thru the serial port (Serial1)

 case 0x05:
        // Signal Strength
        Serial.print("RSSI ");
         Serial.println(buf[8],HEX);
        break;

The 8th byte is the HEX value:

Replace the "HEX" to "DEC" and you will get this byte in decimal:

Serial.println(buf[8],DEC);

You can also omitted the DEC because the decimal form is default for Serial.print():

Serial.println(buf[8]);

 Serial.println(buf[8],HEX);

In this line of code buf[8] is in binary, as is all data in memory. It is the HEX parameter that you are using that causes it to be printed as HEX. Have you by any chance tried printing it without that parameter or even using the BIN parameter ?

I want to know how to calculate or convert it to the value it represents from -130 to -30 with a formula or an array.

What people are trying to tell you, is that it already has the value it represents.

By not following the forum guidelines to post all your code, we are deprived of the information of what data type 'buf[]' is. If it is a char array, you can just print it as suggested in post #11.

Serial.println(buf[8]);

Because, 'char' is a signed 8 bit type.

1 Like

You hex values doesn't contain representation of this range. So the hex to DEC conversion won't resolve your problem.

Your question - is typical XY-problem. Please describe the exact issue, it seems to me that it has nothing to do with hex values.

No it is another value

DEC 75 is -30
DEC 10 is -130

How van i calculate the DEC value to correspond with the out put?
So DEC 50 = - x?

Your question is application specific. There is no general answer for that. A proper answer depends completely on your following the forum guidelines in how to post a question properly

Because:

No it is another value
DEC 75 is -30

Why? Do you think this a common relationship? It isn't. So please be considerate of our time, and fully explain and reveal where these numbers are coming from and what they mean.

So I wrote to you like this - your task is not solved simply by translating hex into dec - there are completely different values
You put the question wrong.

Dec 75 is 75
DEC 10 is 10

If some function f(75) = -30, and f(75) is -130, then the answer was in post 3:

Here's a plot:

The data:

  V1 V2
-130 0xA
-120 0x12
-100 0x25
 -90 0x2D
 -80 0x37
 -70 0x41
 -60 0x4D
 -50 0x57
 -40 0x5F
 -30 0x4B
1 Like

It has nothing to do with HEX or DEC.

I get a value from another device that relates to -30 to -130 , it comes to the Arduino over the serial port .
So the OTHER device sends 4B (DEC 75) , this means the value -30dBm
If the OTHER device sends A (DEC10) ,this means the value is -130dBm.

My question is as follows , can i calculate the value or do i need to use an array or another clever solution to convert the value that is sent by the device to the right value?

The device is a receiver that sends the received signal strength over a serial connection.

Great , maybe -30 4B is wrong
but how do i calculate this in the Arduino code??