Help with byte[8] to long

Discussion in 'Java' started by jeffdeanda@gmail.com, Oct 26, 2005.

  1. Guest

    I'm having trouble converting a byte array to long. The code below
    results in a long value of 4096, which is way off. If I remove on or
    the other values and replace it with 0 it still results in the same
    outcome. I'm thinking there's a problem with the shifting in the 2nd to
    last line, but am unable to figure it out. Any thoughts?

    Here's the code:

    short[] ss = new short[4];
    ss[0] = 0;
    ss[1] = 4096;
    ss[2] = 0;
    ss[3] = 4096;

    byte[] b = new byte[8];
    b[0] = (byte)(ss[0] >> 8 & 0x0FF);
    b[1] = (byte)(ss[0] >> 0 & 0x0FF);
    b[2] = (byte)(ss[1] >> 8 & 0x0FF);
    b[3] = (byte)(ss[1] >> 0 & 0x0FF);
    b[4] = (byte)(ss[2] >> 8 & 0x0FF);
    b[5] = (byte)(ss[2] >> 0 & 0x0FF);
    b[6] = (byte)(ss[3] >> 8 & 0x0FF);
    b[7] = (byte)(ss[3] >> 0 & 0x0FF);

    long l = (long)(
    (b[0] << 56) |
    (b[1] << 48) |
    (b[2] << 40) |
    (b[3] << 32) |
    (b[4] << 24) |
    (b[5] << 16) |
    (b[6] << 8) |
    (b[7] << 0));

    System.out.println(Long.toString(l) + "\n");
     
    , Oct 26, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 25 Oct 2005 18:03:58 -0700, wrote, quoted or
    indirectly quoted someone who said :

    >I'm having trouble converting a byte array to long. The code below
    >results in a long value of 4096, which is way off. If I remove on or
    >the other values and replace it with 0 it still results in the same
    >outcome. I'm thinking there's a problem with the shifting in the 2nd to
    >last line, but am unable to figure it out. Any thoughts?


    Have a look at http://mindprod.com/jgloss/endian.html

    IT contains codes for taking longs apart into bytes and putting them
    together again in the opposite byte sex order.

    You can use half the code.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Oct 26, 2005
    #2
    1. Advertising

  3. Nigel Wade Guest

    wrote:

    > I'm having trouble converting a byte array to long. The code below
    > results in a long value of 4096, which is way off. If I remove on or
    > the other values and replace it with 0 it still results in the same
    > outcome. I'm thinking there's a problem with the shifting in the 2nd to
    > last line, but am unable to figure it out. Any thoughts?
    >
    > Here's the code:
    >
    > short[] ss = new short[4];
    > ss[0] = 0;
    > ss[1] = 4096;
    > ss[2] = 0;
    > ss[3] = 4096;
    >
    > byte[] b = new byte[8];
    > b[0] = (byte)(ss[0] >> 8 & 0x0FF);
    > b[1] = (byte)(ss[0] >> 0 & 0x0FF);
    > b[2] = (byte)(ss[1] >> 8 & 0x0FF);
    > b[3] = (byte)(ss[1] >> 0 & 0x0FF);
    > b[4] = (byte)(ss[2] >> 8 & 0x0FF);
    > b[5] = (byte)(ss[2] >> 0 & 0x0FF);
    > b[6] = (byte)(ss[3] >> 8 & 0x0FF);
    > b[7] = (byte)(ss[3] >> 0 & 0x0FF);
    >
    > long l = (long)(
    > (b[0] << 56) |
    > (b[1] << 48) |
    > (b[2] << 40) |
    > (b[3] << 32) |
    > (b[4] << 24) |
    > (b[5] << 16) |
    > (b[6] << 8) |
    > (b[7] << 0));
    >
    > System.out.println(Long.toString(l) + "\n");


    ByteBuffer bb = ByteBuffer.wrap(b);
    ((ShortBuffer)bb.asShortBuffer()).put(ss);
    long l = bb.getLong();

    --
    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, Oct 26, 2005
    #3
  4. Guest

    The problem is that the << operator returns type int unless the operand
    to its left is a long. Your operands to the left of << are bytes, so
    the result type of each << operation is int, and you're dropping bits
    when you shift left past the 32-bit width of an int. You want your
    left-hand operands to be longs so you have a 64-bit area to work in.

    Try casting your left hand operands to longs, like this:

    long l =
    ((long)b[0] << 56) |
    ((long)b[1] << 48) |
    ((long)b[2] << 40) |
    ((long)b[3] << 32) |
    ((long)b[4] << 24) |
    ((long)b[5] << 16) |
    ((long)b[6] << 8) |
    ((long)b[7] << 0);
     
    , Oct 26, 2005
    #4
  5. Guest

    Thanks for the help. Got it to work.
     
    , Oct 26, 2005
    #5
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    728
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,236
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Replies:
    8
    Views:
    641
    CBFalconer
    Nov 8, 2006
  4. Replies:
    3
    Views:
    522
    Keith Thompson
    Nov 8, 2006
  5. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    506
    santosh
    Jul 24, 2007
Loading...

Share This Page