Solved

Floating Point to byte arrays re visited

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 40
Facing this issue for maven proxy setting 2 27
Java Eclipse Loop 3 31
Strange router problem - can't access hotmail.com 14 56
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

730 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