Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How to write integer in little endian order in java ? (http://www.velocityreviews.com/forums/t642092-how-to-write-integer-in-little-endian-order-in-java.html)

tobleron 10-28-2008 01:03 PM

How to write integer in little endian order in java ?
 
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 10-28-2008 02:38 PM

Re: How to write integer in little endian order in java ?
 
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 10-29-2008 01:32 AM

Re: How to write integer in little endian order in java ?
 
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

Roedy Green 10-29-2008 07:08 AM

Re: How to write integer in little endian order in java ?
 
On Tue, 28 Oct 2008 06:03:37 -0700 (PDT), tobleron <budhik@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 10-29-2008 09:30 AM

Re: How to write integer in little endian order in java ?
 
Roedy Green wrote:

> On Tue, 28 Oct 2008 06:03:37 -0700 (PDT), tobleron <budhik@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 10-30-2008 09:09 PM

Re: How to write integer in little endian order in java ?
 
On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <nmw@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.

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

Arne Vajh°j 10-31-2008 01:29 AM

Re: How to write integer in little endian order in java ?
 
Roedy Green wrote:
> On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <nmw@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 10-31-2008 08:34 AM

Re: How to write integer in little endian order in java ?
 
@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 10-31-2008 09:57 AM

Re: How to write integer in little endian order in java ?
 
Roedy Green wrote:

> On Wed, 29 Oct 2008 09:30:20 +0000, Nigel Wade <nmw@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

beheezie@gmail.com 11-15-2008 06:43 AM

Re: How to write integer in little endian order in java ?
 
On Oct 31, 1:57*am, Nigel Wade <n...@ion.le.ac.uk> wrote:
> 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


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.


All times are GMT. The time now is 03:19 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.