How to write integer in little endian order in java ?

Discussion in 'Java' started by tobleron, Oct 28, 2008.

  1. tobleron

    tobleron Guest

    Hi,

    I have integer :

    a = 4728;
    b = 312000;

    I used ByteBuffer to swicth the byte order into little endian and
    wrote :

    file.writeInt(a);
    file.writeInt(b);
    file.writeShort(a);
    file.writeShort(b);

    And the result are :

    78 12 00 00
    c0 c2 04 00
    78 12
    c0 c2

    I need the result like this :

    00 00 12 78
    04 00 c0 c2

    How to do that ? I must be written in little endian order and
    segmented into 2 bytes fragment. Please advise.

    Best regards.
     
    tobleron, Oct 28, 2008
    #1
    1. Advertising

  2. tobleron

    Nigel Wade Guest

    tobleron wrote:

    > Hi,
    >
    > I have integer :
    >
    > a = 4728;
    > b = 312000;
    >
    > I used ByteBuffer to swicth the byte order into little endian and
    > wrote :
    >
    > file.writeInt(a);
    > file.writeInt(b);
    > file.writeShort(a);
    > file.writeShort(b);


    How would using a ByteBuffer affect the values of a and b?

    >
    > And the result are :
    >
    > 78 12 00 00
    > c0 c2 04 00
    > 78 12
    > c0 c2


    Those are little endian values.

    >
    > I need the result like this :
    >
    > 00 00 12 78
    > 04 00 c0 c2


    That's neither big or little endian.

    Big endian has the MSB first. Little endian has the MSB last. Both have all the
    bytes in the correct sequence.

    The hex value of 4728 is 0x1278 and that of 312000 is 0x4c2c0. A big endian
    value will have the bytes in that order: 00 00 12 78 and 00 04 c2 c0. A little
    endian value will have them reversed: 78 12 00 00 and c0 c2 04 00.

    Unless, of course, you are viewing a 32bit big endian number incorrectly as 2
    16bit little endian values. Be aware that the way you view the resulting data
    file will affect how you perceive the values in the file. If you view the data
    on a little endian machine it will mess with your brain unless you view it as
    bytes.

    For example, I've written those values above to a file as first little endian
    and then big endian. Viewing the file on a little endian machine as bytes I
    get:
    $ od -tx1 data.dat
    78 12 00 00 c0 c2 04 00 00 00 12 78 00 04 c2 c0
    The little endian values which come first have the byte sequence reversed.

    If I view them as 32bit quantities then I get:
    $ od -tx4 data.dat
    00001278 0004c2c0 78120000 c0c20400
    so the little endian values appear correct and the big endian look byte
    reversed.

    If, however, I try to look at them as 16bit quantities then the 16bit words
    appear swapped:
    $ od -tx2 data.dat
    1278 0000 c2c0 0004 0000 7812 0400 c0c2

    The actual data is the same in all cases. All that's changed is the perspective.

    >
    > How to do that ? I must be written in little endian order and
    > segmented into 2 bytes fragment. Please advise.


    If you really want some peculiar mix'n'match byte ordering then you'll have to
    write the bytes into a byte[] yourself in the order you want and then output
    that byte[].


    --
    Nigel Wade
     
    Nigel Wade, Oct 28, 2008
    #2
    1. Advertising

  3. tobleron

    Arne Vajhøj Guest

    tobleron wrote:
    > I have integer :
    >
    > a = 4728;
    > b = 312000;
    >
    > I used ByteBuffer to swicth the byte order into little endian and
    > wrote :
    >
    > file.writeInt(a);
    > file.writeInt(b);
    > file.writeShort(a);
    > file.writeShort(b);
    >
    > And the result are :
    >
    > 78 12 00 00
    > c0 c2 04 00
    > 78 12
    > c0 c2
    >
    > I need the result like this :
    >
    > 00 00 12 78
    > 04 00 c0 c2
    >
    > How to do that ? I must be written in little endian order and
    > segmented into 2 bytes fragment. Please advise.


    I think you need to explain in more detail what you want.

    You write 12 byte and you get 12 byte of output and you say
    you want 8 byte output.

    And even if it is the first 8 shown, then the two ints are swapped
    differently in what you say you want.

    Arne
     
    Arne Vajhøj, Oct 29, 2008
    #3
  4. tobleron

    Roedy Green Guest

    On Tue, 28 Oct 2008 06:03:37 -0700 (PDT), tobleron <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >How to do that ? I must be written in little endian order and
    >segmented into 2 bytes fragment. Please advise.


    the easy way is to use LEDataStream that works exactly like Data
    Stream.

    See http://mindprod.com/products1.html#DATASTREAM
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    A vote for McCain is fearful clinging to McSame.
    A vote for Obama is a shot at Obamalot.
     
    Roedy Green, Oct 29, 2008
    #4
  5. tobleron

    Nigel Wade Guest

    Roedy Green wrote:

    > On Tue, 28 Oct 2008 06:03:37 -0700 (PDT), tobleron <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>
    >>How to do that ? I must be written in little endian order and
    >>segmented into 2 bytes fragment. Please advise.

    >
    > the easy way is to use LEDataStream that works exactly like Data
    > Stream.
    >


    A better way is to use the standard API classes correctly so you are not
    dependent on some external Jar.

    --
    Nigel Wade
     
    Nigel Wade, Oct 29, 2008
    #5
  6. tobleron

    Roedy Green Guest

    On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <>
    wrote, quoted or indirectly quoted someone who said :

    >A better way is to use the standard API classes correctly so you are not
    >dependent on some external Jar.


    LEDataStream works in JDK 1.1. ByteBuffers were not added till
    somewhat later.

    If you just want to get the job done, LEDataStream is much easier than
    learning NIO. It has exactly the same API as
    DataInputStream/DataOutputStream.

    If you are already familiar with NIO, then that would be the
    preferable approach.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    A vote for McCain is fearful clinging to McSame.
    A vote for Obama is a shot at Obamalot.
     
    Roedy Green, Oct 30, 2008
    #6
  7. tobleron

    Arne Vajhøj Guest

    Roedy Green wrote:
    > On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <>
    > wrote, quoted or indirectly quoted someone who said :
    >> A better way is to use the standard API classes correctly so you are not
    >> dependent on some external Jar.

    >
    > LEDataStream works in JDK 1.1. ByteBuffers were not added till
    > somewhat later.
    >
    > If you just want to get the job done, LEDataStream is much easier than
    > learning NIO. It has exactly the same API as
    > DataInputStream/DataOutputStream.
    >
    > If you are already familiar with NIO, then that would be the
    > preferable approach.


    That does not address the point of the extra jar file.

    Arne
     
    Arne Vajhøj, Oct 31, 2008
    #7
  8. tobleron

    tobleron Guest

    @All

    Thank you for your suggestions. I think I was wrong to identify
    endianese of DICOM file. It works now. Thank you all...
     
    tobleron, Oct 31, 2008
    #8
  9. tobleron

    Nigel Wade Guest

    Roedy Green wrote:

    > On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>A better way is to use the standard API classes correctly so you are not
    >>dependent on some external Jar.

    >
    > LEDataStream works in JDK 1.1. ByteBuffers were not added till
    > somewhat later.


    and exactly how many people do you think are still using the (seriously
    deprecated) version 1.1? What relevance does this have?

    >
    > If you just want to get the job done, LEDataStream is much easier than
    > learning NIO. It has exactly the same API as
    > DataInputStream/DataOutputStream.


    The classes are part of the standard API and are no different from any other IO
    classes. There is no such thing a "learning NIO". You just need to read the API
    docs.

    >
    > If you are already familiar with NIO, then that would be the
    > preferable approach.
    >


    I would say is was the preferable approach in all instances given that
    LEDataStream is non-standard, requiring inclusion and shipping of non-standard
    libraries which are subject to license restrictions.

    --
    Nigel Wade
     
    Nigel Wade, Oct 31, 2008
    #9
  10. tobleron

    Guest

    On Oct 31, 1:57 am, Nigel Wade <> wrote:
    > Roedy Green wrote:
    > > On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <>
    > > wrote, quoted or indirectly quoted someone who said :

    >
    > >>A better way is to use the standard API classes correctly so you are not
    > >>dependent on some external Jar.

    >
    > > LEDataStream works in JDK 1.1.  ByteBuffers were not added till
    > > somewhat later.

    >
    > and exactly how many people do you think are still using the (seriously
    > deprecated) version 1.1? What relevance does this have?
    >
    >
    >
    > > If you just want to get the job done, LEDataStream is much easier than
    > > learning NIO.  It has exactly the same API as
    > > DataInputStream/DataOutputStream.

    >
    > The classes are part of the standard API and are no different from any other IO
    > classes. There is no such thing a "learning NIO". You just need to read the API
    > docs.
    >
    >
    >
    > > If you are already familiar with NIO, then that would be the
    > > preferable approach.

    >
    > I would say is was the preferable approach in all instances given that
    > LEDataStream is non-standard, requiring inclusion and shipping of non-standard
    > libraries which are subject to license restrictions.
    >
    > --
    > Nigel Wade


    Nice discussion, but you forgot to answer the person's question.

    "read the API docs" isn't exactly a helpful answer. I have read
    through them and it seems like most of the internet and am still
    having trouble.

    Can anyone give me a little help on how to switch short, int, float
    from short endian to big endian?

    I am reading a DataInputStream, but the data is coming in "backward"
    and java isn't outputting the value in the way I would like.

    Thanks.
     
    , Nov 15, 2008
    #10
  11. In article
    <>,
    wrote:
    [...]

    > Can anyone give me a little help on how to switch short, int, float
    > from [little] endian to big endian?


    This article discusses int:

    <http://groups.google.com/group/comp.lang.java.programmer/msg/0c4089c32c2578a0?hl=en>

    See also:

    <http://java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html>

    [...]
    --
    John B. Matthews
    trashgod at gmail dot com
    http://home.roadrunner.com/~jbmatthews/
     
    John B. Matthews, Nov 15, 2008
    #11
  12. tobleron

    Arne Vajhøj Guest

    wrote:
    > Nice discussion, but you forgot to answer the person's question.
    >
    > "read the API docs" isn't exactly a helpful answer. I have read
    > through them and it seems like most of the internet and am still
    > having trouble.
    >
    > Can anyone give me a little help on how to switch short, int, float
    > from short endian to big endian?
    >
    > I am reading a DataInputStream, but the data is coming in "backward"
    > and java isn't outputting the value in the way I would like.


    There are two simple solutions:

    1) Use ByteBuffer and call the order method to set
    the endianness you want.

    2) Do as you do but flip the bytes yourself. It is trivial
    to flip short and int - and the Float class has methods
    to convert between float and int.

    Arne
     
    Arne Vajhøj, Nov 15, 2008
    #12
  13. tobleron

    Roedy Green Guest

    On Fri, 14 Nov 2008 22:43:15 -0800 (PST), wrote,
    quoted or indirectly quoted someone who said :

    >
    >I am reading a DataInputStream, but the data is coming in "backward"
    >and java isn't outputting the value in the way I would like.


    if you just want to get the job done, see
    http://mindprod.com/products1.html#LEDATASTREAM

    To use it, see http://mindprod.com/applet/fileio.html
    to generate you the code.

    If for some reason you don't like that easy solution, see
    http://mindprod.com/jgloss/endian.html which will give you several
    more difficult ones.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Your old road is
    Rapidly agin'.
    Please get out of the new one
    If you can't lend your hand
    For the times they are a-changin'.
     
    Roedy Green, Nov 15, 2008
    #13
  14. tobleron

    Roedy Green Guest


    >> The classes are part of the standard API and are no different from any other IO
    >> classes. There is no such thing a "learning NIO". You just need to read the API
    >> docs.


    That is sort of like handing a child a 5000 piece Lego set and saying
    "build me a model Duesenberg with working cylinders".

    NIO is a has many parts each individually documented and not much docs
    on how to use them. It takes a day or two of fiddling around to see
    what everything is for.

    That process is what I call "learning NIO".


    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Your old road is
    Rapidly agin'.
    Please get out of the new one
    If you can't lend your hand
    For the times they are a-changin'.
     
    Roedy Green, Nov 15, 2008
    #14
  15. wrote:
    > I am reading a DataInputStream, but the data is coming in "backward"
    > and java isn't outputting the value in the way I would like.


    Assuming that you are at Java 5.0, at least, this is the approved way to
    do it:

    short s = Short.reverseBytes(dis.readShort());
    int i = Integer.reverseBytes(dis.readInt());
    long l = Long.reverseBytes(dis.readLong());
    float f = Float.intBitsToFloat(Integer.reverseBytes(dis.readInt());
    double d = Double.longBitsToDouble(Long.reverseBytes(dis.readLong());


    --
    John W. Kennedy
    "Sweet, was Christ crucified to create this chat?"
    -- Charles Williams. "Judgement at Chelmsford"
     
    John W Kennedy, Nov 16, 2008
    #15
  16. tobleron

    Nigel Wade Guest

    wrote:

    >
    > Nice discussion, but you forgot to answer the person's question.


    That's because the original question didn't make sense. The format the OP
    specified they wanted was neither big nor little endian. I asked for
    clarification, got none, so moved on.

    >
    > "read the API docs" isn't exactly a helpful answer. I have read
    > through them and it seems like most of the internet and am still
    > having trouble.


    That answer wasn't addressed to the OP.

    >
    > Can anyone give me a little help on how to switch short, int, float
    > from short endian to big endian?
    >
    > I am reading a DataInputStream, but the data is coming in "backward"
    > and java isn't outputting the value in the way I would like.


    This is one way:

    Read the data into a byte array.
    Create a ByteBuffer and set its order to LITTLE_ENDIAN.
    Wrap the ByteBuffer around the byte array.
    Read the data from the ByteBuffer.

    The above approach is most useful if you are reading complex data structures,
    for example scientific data, where you typically read a complete "record" in a
    single operation. Once you've read the "record" into the byte array you can
    then extract the relevant data fields via the ByteBuffer.

    This is code extracted from a class which reads little-endian data from a file:

    this.file = new File( filename );

    // get an InputStream on the file
    in = new FileInputStream( file );

    // get a DataInputStream which can be used to read words.
    // if it's gzipped wrap it in a ZipInputStream
    if ( fileName.endsWith( ".gz" ) )
    dataIn = new DataInputStream( new GZIPInputStream( in ) );
    else
    dataIn = new DataInputStream( in );

    .... some time later

    byte[] lengthBytes = new byte[4];
    dataIn.readFully( lengthBytes );

    // wrap the byte array in a ByteBuffer, this allows
    // reading of little endian data
    ByteBuffer bb = ByteBuffer.wrap( lengthBytes );
    bb.order( ByteOrder.LITTLE_ENDIAN );

    fitRecordLength = bb.getShort();
    inxRecordLength = bb.getShort();

    .... and later still

    bufferArray = new byte[fitRecordLength];
    dataBuffer = ByteBuffer.wrap( bufferArray );
    dataBuffer.order( ByteOrder.LITTLE_ENDIAN );

    dataIn.readFully( bufferArray );
    dataBuffer.position( 0 );

    int recordNumber = dataBuffer.getInt();
    int r_time = dataBuffer.getInt();

    etc.

    --
    Nigel Wade
     
    Nigel Wade, Nov 18, 2008
    #16
    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,028
    dxcoder
    Jul 2, 2003
  2. Ernst Murnleitner

    float: IEEE, big endian, little endian

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

    Little Endian to Big Endian

    invincible, Jun 14, 2005, in forum: C++
    Replies:
    9
    Views:
    14,355
    Old Wolf
    Jun 14, 2005
  4. invincible
    Replies:
    1
    Views:
    547
    red floyd
    Jun 14, 2005
  5. pallavi

    .Net webservice and java client little endian/big endian

    pallavi, Apr 5, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    277
    pallavi
    Apr 5, 2004
Loading...

Share This Page