## convert two Int to one Float

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

George_B
Posts: 112
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 43 times
Been thanked: 16 times
Contact:

### convert two Int to one Float

Hello to everyone !

In modbus, sometimes, we need to obtain float variables to describe a value.

As i have understand so far there will be need to send two integer variables in order to describe this float variable.

My question is, if i have a float variable = 24.256, how it is possible to split that into two integers ?

Thank in advance !

George

stefan.erni
Valued Contributor
Posts: 650
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 178 times
Contact:

### Re: convert two Int to one Float

Hi George

If you only have this value that you want to convert from float to integer then multiply the value by 1000 then you have an integer. You just have to divide by 1000 somewhere.

regards

Stefan

George_B
Posts: 112
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 43 times
Been thanked: 16 times
Contact:

### Re: convert two Int to one Float

Hi Stafan, nice to hear from you.

Maybe i was not describe the issue with details.

In Modbus, when you define the address you read as float type, it will automatically read two addresses with one integer on each address in order to obtain the float value.

For example:
If you want to read the register 40001 as float then,

you have to read address 40001 as int
you also have to read address 40002 as int

then you have to union those two to end up with the float value.

Since i want to implement my program from a slave modbus device perspective, i need to do the opposite from what i describe above.
I have the float value and i need to send the two integer values through modbus fo as a float value will be obtained in the modbus master device .

Another example is that:

Int_var_1 = 16992
Int_var_2 = -32768

Float_var = 56.125

I can not figure out how to calculate the Int_var_1 and Int_var_2 when i know the Float_var = 56.125

Best Regards
George

Steve
Matrix Staff
Posts: 3305
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 109 times
Been thanked: 418 times
Contact:

### Re: convert two Int to one Float

It sounds like the FLOAT is being represented by 4 bytes. An INT is 2 bytes and so reading the 2 INTs gives you the 4 bytes representing the FLOAT.

Once you have these 4 bytes, you need to know how they represent the FLOAT. For example, see here:
https://en.wikipedia.org/wiki/Single-pr ... int_format

You will also have to understand how these 4 bytes are ordered. This is called "endianness":
https://en.wikipedia.org/wiki/Endianness

viewtopic.php?f=7&t=21810

So it's complicated! But we have a component in Flowcode (v8 and v9) that should help with this conversion. It's called "TypeConversion".

Hope this helps.
Steve.

George_B
Posts: 112
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 43 times
Been thanked: 16 times
Contact:

### Re: convert two Int to one Float

Hi Steve, thanks for the reply and help.

I am using Flowcode 7, at the moment i can not use FC8.

I thought it would be easy somehow to implement this conversion using C code.

I have seen this post with 4 bytes but i could not figure out any result since in my case i need to use two integer values.

Regards
George

Benj
Matrix Staff
Posts: 15197
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4762 times
Been thanked: 4296 times
Contact:

### Re: convert two Int to one Float

Hi George,

This topic should help.
viewtopic.php?f=28&t=12561&p=49907

In your case the union could instead look like this.

Code: Select all

``````typedef union
{
MX_FLOAT AsFloat;
MX_UINT16  AsInt[2];
} MX_UnionFloat;``````

George_B
Posts: 112
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 43 times
Been thanked: 16 times
Contact:

### Re: convert two Int to one Float

hi Benj

Thank you very much for your reply.

This is the Union as you said.

In my case ineed to do the "Seperation" from one Float to two Integers.

Would it be the same as it is for bytes?

Code: Select all

``````Temp.AsByte[0] = FCV_BYTE0;
Temp.AsByte[1] = FCV_BYTE1;
Temp.AsByte[2] = FCV_BYTE2;
Temp.AsByte[3] = FCV_BYTE3;
FCV_FVAR = Temp.AsFloat;``````

Benj
Matrix Staff
Posts: 15197
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4762 times
Been thanked: 4296 times
Contact:

### Re: convert two Int to one Float

Hello,

In your case it would look like this.

Code: Select all

``````Temp.AsInt[0] = FCV_INT0;
Temp.AsInt[1] = FCV_INT1;
FCV_FVAR = Temp.AsFloat;``````