?
Solved

Floating Point to byte arrays re visited

Posted on 2006-10-23
3
Medium Priority
?
234 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
[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
3 Comments
 
LVL 9

Accepted Solution

by:
shinobun earned 200 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 100 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 19 hours left to enroll

752 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