32-bit hex to 32-bit floating point conversion

K

khodidas

I'm reading an temperature controller which communicates on modbus RTU protocol. I am getting data as 32-bit wide i.e. two register of the modbus.
lets say 40001=719CH
and 40002=44F2H
so 32 bit data "44F2719C".
i.e. in decimal 1939.5503

I need routine in VB or C to convert this string into 32-bit float. Can anyone help me?

R

Robert Scott

You already have the data in floating point form. All that is needed is to make the C program recognize the data as a float. You can use a union or a cast in C. Here is how you can do it using a cast. Suppose that the result is in two 16-bit short integers, resLO and resHI, and suppose that x is a float.

resLO = 0x719c;
resHI = 0x44f2; //..(or however you get them)…

((short int*)(&x))[0] = resLO;
((short int*)(&x))[1] = resHI;

//..Now x has the float value in it.

(short int*) tells C to treat the address of x as if it were the beginning of an array of short ints. [0] fills the first 16 bits and [1] fills the second 16 bits. There are other ways of doing this. Essentially what you need to do is to trick C into looking at the same data in two different ways - as a pair of 16-bit short ints and as a single 32-bit float.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting

K

khodidas

Respected sir,

thank for this reply.

with regards,
khodidas

A

Asier Illaro

I have to convert a float number to his HEX representation, but I don't know if this can be done using a casting or if I need to build a routine to do that, (managing the mantisse... stuff).

Thank you very much,
Sorry for my bad English,

Asier Illaro
---------------------------------------------------
Electronic Department
Análisis y Simulación S.L.

S

sam

Can you show the same result in VB instead of C?

Thanks!!!