Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Encode float as int

Posted on 2007-11-23
4
Medium Priority
?
532 Views
Last Modified: 2012-06-21
Hi

I am integrating an existing protocol, that does not have support for floats, but only 16 bit unsigned shorts.

Seeing the values I want to encode are really small (max =30), I thought to use the 8 MSB for the integer part, and the 8 LSB for the fractal part.

The problems arise however as to how do I encode 3.5 differently then 3.05

Any ideas anyone?

0
Comment
Question by:x_terminat_or_3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1800 total points
ID: 20339917
What you could do is store 3.5 as 3500 and 3.05 as 3050

That way, you can store the range -30.000 to 30.000 as the integers -30000 to 30000.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1800 total points
ID: 20339926
It also makes it easy for addition and subtraction ... nothing special to do, just add the ints. Multiplication and division are slightly more complicated, but not much.

And for output, you can simply do mod 1000 to get the part after the comma, and div 1000 to get the part before the comma.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 200 total points
ID: 20339979
in your original idea, 3.5 should be stored as 3 and 50, while 3.05 is stored as 3 and 5
But infinity's proposal is more commonly used in this case.
0
 
LVL 2

Author Closing Comment

by:x_terminat_or_3
ID: 31410686
:-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question