Solved

Floating Point to byte arrays re visited

Posted on 2006-10-23
3
225 Views
Last Modified: 2008-02-01
Ok. Recently some of you suggested how to convert a float number to a 4-byte array, and vice-versa.
and they worked well.

Now I am doing the same for 8-byte double numbers. I have writen the code and the class (some of the stuff is removed from this display).
Here is the class and the main() program. Can any experts in floating-point manipulation here tell why I am getting NaN when I ran this
example?

public class DoubleToByteArray
{
 public static byte[] toByteArray(double doubleVal)
   {
     byte[] dByteArray = new byte[8];
     long n = Double.doubleToLongBits(doubleVal);

     for(int i = 0;  i < dByteArray.length;  i++)
     {
        dByteArray[i] = (byte)(n >> (i * 8) &0xFF);  
     }
     
     return dByteArray;            
   }      
      
 
   public static double toDoubleValue(byte[] inBytes)
   {
     long n = 0;
     for(int i = 0;  i < inBytes.length;  i++)
     {
        n |= ((inBytes[i] & 0377) << (i * 8));
     }
     
     double doubleValue = Double.longBitsToDouble(n);

     return doubleValue;      
   }      
      
   public static void main(String [] args)
   {      
     double aValue = -46633.91;
     
     byte[] doubleBytes = ByteArray.toByteArray(aValue);
     
     double recreatedValue = ByteArray.toDoubleValue(doubleBytes);
     
     System.out.println("recreated : " + recreatedValue);
  }
}
0
Comment
Question by:prain
3 Comments
 
LVL 9

Accepted Solution

by:
shinobun earned 50 total points
ID: 17793262
((inBytes[i] & 0377) << (i * 8)) is being calculated as an int.  You need to cast it to a long:

 ((long) (inBytes[i] & 0377) << (i * 8));
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 25 total points
ID: 17794117
Just a recommendation, prevent excessive multiplication :)

// DoubleToByteArray
     for (int i = 0, shiftBy = 0; shiftBy < 64; shiftBy += 8) {
        dByteArray[i++] = (byte) (n >> shiftBy & 0xFF);  
     }

// toDoubleValue
     for (int i = 0, shiftBy = 0; shiftBy < 64; shiftBy += 8) {
        n |= (long) (inBytes[i++] & 0xFF) << shiftBy;
     }
0
 

Author Comment

by:prain
ID: 17795609
Thanks both of you. Hoomanv, thanks for the nice improved code.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

744 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

13 Experts available now in Live!

Get 1:1 Help Now