Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Float to Integer conversion

Status
Not open for further replies.

dhakeparag81

Full Member level 2
Full Member level 2
Joined
Jun 6, 2012
Messages
142
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
INDIA
Visit site
Activity points
2,469
hello there,

I wrote modbus application for pic81f87k22 cpu that support only integer value.
Might be you know that in float msb is signed 8 exponent and 23 mantisa i got that much of knowledge from documentation.

I want to write function in c that convert integer value to float and vice versa.

What is the procedure to convert float to integer?
Do you have any document on that or c code?

Please note that im not talking about type casting, because i want to transfer these data through modbus and other hand there may be any device which support modbus protocol with IEEE 754 for float.
 

I'm not sure what exactly you mean?

In order to convert a float to integer you truncate all the decimals so what's wrong with (long int)my_float ?
If you want a round effect then use (unsigned int) (my_float+0.5) for positives.
If the float can be negative too my_float >= 0 ? (long int)(my_float+0.5) : (long int)(my_float-0.5)

Also in order to convert an integer to a float use (float)my_integer.

If you cant to keep more decimals you can multiply the float with a power of 10 before the conversion

Are you after a different thing?
 

what i want is if you establish communication between controller and PLC (programmable logic control) and if plc sends controller data in float it comes in 32 bit
that is in two integer then controller have to convert data into float, signed bit mantisa .... all that stuff which i dont know
 

So you mean to receive two 16bit values and reconstruct the float that was send?

p.s. I just saw that you already said "Please note that im not talking about type casting" so my reply was off topic
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top