Double troubles

G

GeniousFayaz

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;
//System.out.println(java.lang.Byte.toString(arr) + " "
+ i);
cnt++;
}
long accum = 0;
i = 0;
for ( int shiftBy = 0; shiftBy < 64; shiftBy += 8 ) {
accum |= ( (long)( tmp & 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
 
I

Ingo R. Homann

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
 
I

Ingo R. Homann

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
 
P

Patricia Shanahan

Ingo said:
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;
}

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

Patricia
 
I

Ingo R. Homann

Hi Patricia,

Patricia said:
Ingo said:
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;
}

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

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
 
P

Patricia Shanahan

Ingo said:
Hi Patricia,

Patricia said:
Ingo said:
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;
}

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

Patricia

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
 
N

Nigel Wade

GeniousFayaz said:
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;
//System.out.println(java.lang.Byte.toString(arr) + " "
+ i);
cnt++;
}
long accum = 0;
i = 0;
for ( int shiftBy = 0; shiftBy < 64; shiftBy += 8 ) {
accum |= ( (long)( tmp & 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.



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();
 
I

Ingo R. Homann

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
 
O

Oliver Wong

GeniousFayaz said:
I have 8 bytes and
I need to convert them to a single double. [...]
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top