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 ?

 
 
tobleron
Guest
Posts: n/a
 
      10-28-2008
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.
 
Reply With Quote
 
 
 
 
Nigel Wade
Guest
Posts: n/a
 
      10-28-2008
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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      10-29-2008
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-29-2008
On Tue, 28 Oct 2008 06:03:37 -0700 (PDT), tobleron <(E-Mail Removed)>
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.
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      10-29-2008
Roedy Green wrote:

> On Tue, 28 Oct 2008 06:03:37 -0700 (PDT), tobleron <(E-Mail Removed)>
> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-30-2008
On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <(E-Mail Removed)>
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.
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      10-31-2008
Roedy Green wrote:
> On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <(E-Mail Removed)>
> 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
 
Reply With Quote
 
tobleron
Guest
Posts: n/a
 
      10-31-2008
@All

Thank you for your suggestions. I think I was wrong to identify
endianese of DICOM file. It works now. Thank you all...
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      10-31-2008
Roedy Green wrote:

> On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <(E-Mail Removed)>
> 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
 
Reply With Quote
 
beheezie@gmail.com
Guest
Posts: n/a
 
      11-15-2008
On Oct 31, 1:57*am, Nigel Wade <(E-Mail Removed)> wrote:
> Roedy Green wrote:
> > On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <(E-Mail Removed)>
> > 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.
 
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