Solved

Floating Point to byte arrays re visited

Posted on 2006-10-23
3
230 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

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

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 42
type mismatch (Object[] to double[] 4 33
hibernate jars 4 45
maven not picking latest jar instead picking old jar from .m2 12 17
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

790 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