Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to write integer in little endian order in java ?

Reply
Thread Tools

How to write integer in little endian order in java ?

 
 
John B. Matthews
Guest
Posts: n/a
 
      11-15-2008
In article
<(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      11-15-2008
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-15-2008
On Fri, 14 Nov 2008 22:43:15 -0800 (PST), (E-Mail Removed) 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'.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-15-2008

>> 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'.
 
Reply With Quote
 
John W Kennedy
Guest
Posts: n/a
 
      11-16-2008
(E-Mail Removed) 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.read Int());
double d = Double.longBitsToDouble(Long.reverseBytes(dis.read Long());


--
John W. Kennedy
"Sweet, was Christ crucified to create this chat?"
-- Charles Williams. "Judgement at Chelmsford"
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      11-18-2008
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Little Endian to Big Endian invincible C++ 9 06-14-2005 10:21 PM
Little Endian to Big Endian for 32 bit invincible C++ 1 06-14-2005 04:20 PM
.Net webservice and java client little endian/big endian pallavi ASP .Net Web Services 0 04-05-2004 07:21 PM
float: IEEE, big endian, little endian Ernst Murnleitner C++ 0 01-13-2004 01:48 PM
convert from BIG-ENDIAN to LITTLE-ENDIAN hicham C++ 2 07-02-2003 04:55 PM



Advertisments