Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Double troubles

Thread view: 
GeniousFayaz - 16 Mar 2007 10:43 GMT
Hi,

I am new to java and I have troubles with doubles. I have 8 bytes and
I need to convert them to a single double. I searched the net and
found out this piece of code
(with all respect to the Author and his rights)

<code>
public static double arr2double(byte[] arr, int start) {
       int i = 0;
       int len = 8;
       int cnt = 0;
       byte[] tmp = new byte[len];
       for (i = start; i < (start + len); i++) {
           tmp[cnt] = arr[i];
           //System.out.println(java.lang.Byte.toString(arr[i]) + " "
+ i);
           cnt++;
       }
       long accum = 0;
       i = 0;
       for ( int shiftBy = 0; shiftBy < 64; shiftBy += 8 ) {
           accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
           i++;
       }
</code>

but when I gave the following array
<code>
   byte[] byteArray = new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x1};
</code>

instead of returning the value 1 (one) the above method returned me
some other
value. I even tried going through IEEE 754 standards but they were
absolutely
out of my scope.

Can any one guide me in "How to convert the given byte array into a
double value" ?

Thanks in advance for all Ur efforts
-F
Ingo R. Homann - 16 Mar 2007 10:58 GMT
Hi Genious,

I am not quite sure what you are talking about, because you talk of
'doubles' but you code shows 'long'.

Anyhow, I think you use the bytes in a reversed order! And it is not
necessary to do it in two loops.

If you really want to convert the bytes to a double, perhaps, the
following methods might help you:

Double.doubleToLongBits(double value)
Double.doubleToRawLongBits(double value)
Double.longBitsToDouble(long bits)

Ciao,
Ingo
Ingo R. Homann - 16 Mar 2007 11:04 GMT
Hi again,

A more readable (and I hope, correct) code would be:

public static long arrayToLong(byte[] arr, int start) {
  long l=0;
  for(int i=0;i<8;i++) {
    l|=arr[start+i]<<(7-i);
  }
  return l;
}

Ciao,
Ingo
Patricia Shanahan - 16 Mar 2007 13:48 GMT
> Hi again,
>
[quoted text clipped - 7 lines]
>   return l;
> }

Suppose arr[start+7] is -1. What will the result be?

Patricia
Ingo R. Homann - 16 Mar 2007 14:42 GMT
Hi Patricia,

>> Hi again,
>>
[quoted text clipped - 11 lines]
>
> Patricia

I am not sure if I understand you correctly, but in my code there is
missing a cast from byte to long, yes.

Ciao,
Ingo
Patricia Shanahan - 16 Mar 2007 14:48 GMT
> Hi Patricia,
>
[quoted text clipped - 16 lines]
> I am not sure if I understand you correctly, but in my code there is
> missing a cast from byte to long, yes.

I think you are missing a masking operation, such as "&". When you cast
the -1 byte to long you will get the long representation of -1, not 255.

Patricia
Ingo R. Homann - 19 Mar 2007 09:12 GMT
Hi,

> I think you are missing a masking operation, such as "&". When you cast
> the -1 byte to long you will get the long representation of -1, not 255.

OK, that's right.

My point was that Genious' code was far to complicated and the two loops
and many variables were not necessary.

On the other hand, my code was not complicated enough ;-) and it would
have been a good idea to test it before posting.

Ciao,
Ingo
Nigel Wade - 16 Mar 2007 16:30 GMT
> Hi,
>
[quoted text clipped - 32 lines]
> some other
> value.

It won't. That's not the byte representation of the double 1.0.

> I even tried going through IEEE 754 standards but they were
> absolutely
> out of my scope.

> Can any one guide me in "How to convert the given byte array into a
> double value" ?

ByteBuffer bb = ByteBuffer.wrap(arr);
double = bb.getDouble();

and if the bytes represent a little-endian double then:

ByteBuffer bb = ByteBuffer.wrap(arr);
bb.order( ByteOrder.LITTLE_ENDIAN );
double = bb.getDouble();

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Oliver Wong - 21 Mar 2007 15:45 GMT
> I have 8 bytes and
> I need to convert them to a single double.
[...]
> but when I gave the following array
> <code>
[quoted text clipped - 7 lines]
> absolutely
> out of my scope.

http://www.psc.edu/general/software/packages/ieee/ieee.html explains IEEE
754 in as close to "plain English" as is reasonably possible for this
topic. In particular, they explain why the byte sequence
0x0000000000000001 does not equal the double value 1.0

   There are many encoding systems for mapping from bytes to double and
back. You need to re-evaluated whether you want to use the IEEE 754
encoding specifically, or whether you want to use some special encoding
where 0x0000000000000001 maps onto 1.0. The two are mutually exclusive.

   - Oliver.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.