Solved

Floating Point to byte arrays re visited

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
xampp tool 12 50
HashTable highest marks enumeration alternative 9 38
SHA2 certs for IIS AND Java? 2 90
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…
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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