Solved

Encode float as int

Posted on 2007-11-23
4
510 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

763 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

9 Experts available now in Live!

Get 1:1 Help Now