Big versus little endian (byte order)

Discussion in 'Java' started by Gido, Jul 5, 2003.

  1. Gido

    Gido Guest

    My questions is: How can I read little endian values from a file stream?

    I am trying to read values from a binary file in the WINDOWS platform.
    The file includes values of type integer (signed 32-bit integer - 4 bytes) and
    double (signed 64-bit IEEE double-precision floating point numbers).
    I am using the readInt() and readDouble() functions as defined in the
    java.io.* package. However, the values are stored in big and little byte order.
    The readInt() and readDouble() functions correctly return only the big endian
    values.

    My questions is: How can I read the little endian values?
     
    Gido, Jul 5, 2003
    #1
    1. Advertising

  2. Gido

    xarax Guest

    (Gido) wrote in message news:<>...
    > My questions is: How can I read little endian values from a file stream?
    >
    > I am trying to read values from a binary file in the WINDOWS platform.
    > The file includes values of type integer (signed 32-bit integer - 4 bytes) and
    > double (signed 64-bit IEEE double-precision floating point numbers).
    > I am using the readInt() and readDouble() functions as defined in the
    > java.io.* package. However, the values are stored in big and little byte order.
    > The readInt() and readDouble() functions correctly return only the big endian
    > values.
    >
    > My questions is: How can I read the little endian values?


    The java.nio (new I/O) package that is new with jdk1.4 has ByteBuffer
    and friends that support big and little endian. Use the new I/O
    facilities.
     
    xarax, Jul 6, 2003
    #2
    1. Advertising

  3. Gido

    Gido Guest

    Jon

    Thanks a lot. Your information was exactly the jump start I needed.
    From here on it will be easy.

    Thanks
    Gido


    "Jon A. Cruz" <> wrote in message news:<>...
    > Gido wrote:
    > > My questions is: How can I read little endian values from a file stream?

    >
    > Either use someone else's class (like Roedy's LEDataStream
    > http://mindprod.com/jgloss/ledatastream.html), or just do it yourself.
    >
    > Read 4 bytes. Shift and mask. Voila! You're done. It should end up
    > looking just like the C/C++ code should.
    >
    >
    > byte tmp[4] = {0,0,0,0};
    > in.readFully( tmp );
    > int val = ((0x0ff & tmp[0]) << 0)
    > | ((0x0ff & tmp[1]) << 8)
    > | ((0x0ff & tmp[2]) <<16)
    > | ((0x0ff & tmp[3]) <<24);
    >
    >
    >
    > And to read a Double
    >
    > Read 8 bytes.
    > Pack them back into a long. Just like that int code but the values keep
    > going.
    >
    > Then
    >
    > Double dbl = Double.longBitsToDouble( val );
     
    Gido, Jul 6, 2003
    #3
  4. Gido

    Gido Guest

    Roedy

    Your web site is a gold mine! Thanks so much for your generous contributions.

    Thanks
    Gido


    Roedy Green <> wrote in message news:<>...
    > On 5 Jul 2003 11:03:55 -0700, (Gido) wrote or
    > quoted :
    >
    > >I am trying to read values from a binary file in the WINDOWS platform.

    >
    > see http://mindprod.com/jgloss/endian.html
     
    Gido, Jul 6, 2003
    #4
    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. hicham
    Replies:
    2
    Views:
    9,037
    dxcoder
    Jul 2, 2003
  2. Ernst Murnleitner

    float: IEEE, big endian, little endian

    Ernst Murnleitner, Jan 13, 2004, in forum: C++
    Replies:
    0
    Views:
    881
    Ernst Murnleitner
    Jan 13, 2004
  3. invincible

    Little Endian to Big Endian

    invincible, Jun 14, 2005, in forum: C++
    Replies:
    9
    Views:
    14,378
    Old Wolf
    Jun 14, 2005
  4. invincible
    Replies:
    1
    Views:
    552
    red floyd
    Jun 14, 2005
  5. Replies:
    5
    Views:
    373
    Stephen Sprunk
    Aug 31, 2006
Loading...

Share This Page