Double troubles

Discussion in 'Java' started by GeniousFayaz, Mar 16, 2007.

  1. GeniousFayaz

    GeniousFayaz Guest

    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
    GeniousFayaz, Mar 16, 2007
    #1
    1. Advertising

  2. 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, Mar 16, 2007
    #2
    1. Advertising

  3. 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
    Ingo R. Homann, Mar 16, 2007
    #3
  4. Ingo R. Homann wrote:
    > 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
    Patricia Shanahan, Mar 16, 2007
    #4
  5. Hi Patricia,

    Patricia Shanahan wrote:
    > Ingo R. Homann wrote:
    >
    >> 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
    Ingo R. Homann, Mar 16, 2007
    #5
  6. Ingo R. Homann wrote:
    > Hi Patricia,
    >
    > Patricia Shanahan wrote:
    >> Ingo R. Homann wrote:
    >>
    >>> 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
    Patricia Shanahan, Mar 16, 2007
    #6
  7. GeniousFayaz

    Nigel Wade Guest

    GeniousFayaz wrote:

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


    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Mar 16, 2007
    #7
  8. 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
    Ingo R. Homann, Mar 19, 2007
    #8
  9. GeniousFayaz

    Oliver Wong Guest

    "GeniousFayaz" <> wrote in message
    news:...
    > 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.
    Oliver Wong, Mar 21, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web learner

    from List <double> to double[]

    Web learner, Apr 25, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    480
  2. sb
    Replies:
    4
    Views:
    305
    Alberto Barbati
    Feb 19, 2004
  3. Jacek Dziedzic
    Replies:
    5
    Views:
    384
    Old Wolf
    Apr 8, 2004
  4. ferran
    Replies:
    9
    Views:
    3,030
    Kevin Goodsell
    Apr 12, 2004
  5. Sydex
    Replies:
    12
    Views:
    6,489
    Victor Bazarov
    Feb 17, 2005
Loading...

Share This Page