Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to write integer in little endian order in java ?

1,684 views
Skip to first unread message

tobleron

unread,
Oct 28, 2008, 9:03:37 AM10/28/08
to
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.

Nigel Wade

unread,
Oct 28, 2008, 10:38:00 AM10/28/08
to
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

Arne Vajhøj

unread,
Oct 28, 2008, 9:32:13 PM10/28/08
to

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

Roedy Green

unread,
Oct 29, 2008, 3:08:36 AM10/29/08
to
On Tue, 28 Oct 2008 06:03:37 -0700 (PDT), tobleron <bud...@yahoo.com>
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.

Nigel Wade

unread,
Oct 29, 2008, 5:30:20 AM10/29/08
to
Roedy Green wrote:

> On Tue, 28 Oct 2008 06:03:37 -0700 (PDT), tobleron <bud...@yahoo.com>
> 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

Roedy Green

unread,
Oct 30, 2008, 5:09:48 PM10/30/08
to
On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <n...@ion.le.ac.uk>

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.

Arne Vajhøj

unread,
Oct 30, 2008, 9:29:55 PM10/30/08
to
Roedy Green wrote:
> On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <n...@ion.le.ac.uk>
> 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

tobleron

unread,
Oct 31, 2008, 4:34:12 AM10/31/08
to
@All

Thank you for your suggestions. I think I was wrong to identify
endianese of DICOM file. It works now. Thank you all...

Nigel Wade

unread,
Oct 31, 2008, 5:57:09 AM10/31/08
to
Roedy Green wrote:

> On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <n...@ion.le.ac.uk>
> 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

behe...@gmail.com

unread,
Nov 15, 2008, 1:43:15 AM11/15/08
to

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.

John B. Matthews

unread,
Nov 15, 2008, 4:39:45 AM11/15/08
to
In article
<a6e72686-7aae-495d...@b38g2000prf.googlegroups.com>,
behe...@gmail.com 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/

Arne Vajhøj

unread,
Nov 15, 2008, 9:08:10 AM11/15/08
to
behe...@gmail.com 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

Roedy Green

unread,
Nov 15, 2008, 11:13:14 AM11/15/08
to
On Fri, 14 Nov 2008 22:43:15 -0800 (PST), behe...@gmail.com 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

unread,
Nov 15, 2008, 11:16:39 AM11/15/08
to

>> 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

John W Kennedy

unread,
Nov 16, 2008, 12:39:57 AM11/16/08
to
behe...@gmail.com 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"

Nigel Wade

unread,
Nov 18, 2008, 9:37:51 AM11/18/08
to
behe...@gmail.com 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

0 new messages