How to write integer in little endian order in java ?

T

tobleron

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

Nigel Wade

tobleron said:
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[].
 
A

Arne Vajhøj

tobleron said:
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
 
N

Nigel Wade

Roedy said:
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.
 
R

Roedy Green

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

Arne Vajhøj

Roedy said:
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
 
T

tobleron

@All

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

Nigel Wade

Roedy said:
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.
 
B

beheezie

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




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.




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.

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

Arne Vajhøj

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
 
R

Roedy Green

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'.
 
R

Roedy Green

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'.
 
J

John W Kennedy

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

Nigel Wade

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top