Solved

Encode float as int

Posted on 2007-11-23
4
515 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
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 450 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 450 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Looking for example pivot year code used in Y2K 4 62
C# Error - Add Failed 12 78
Recommendation vb6 to vb.net or others 14 112
Programatically extract date from website 8 67
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now