LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-10-2006, 01:53 AM   #1
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Rep: Reputation: 35
How to binary-write in Java in little endian?


Now I use:
Code:
File file = new File ("__smth__");
            try {
                FileOutputStream file_out = new FileOutputStream (file);
                DataOutputStream out = new DataOutputStream (file_out);
                out.writeInt (123);
                //out.writeFloat (3.14159f);
                file_out.close ();
            }
            catch (IOException ex) {
                System.out.println ("IO exception: " + ex);
            }
But it's in Big Endian! I need it in Little Endian. Can anyone give me an example of binary-writing an int to a file in little endian?
Thanks.
 
Old 12-10-2006, 02:10 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Java deliberately insulates you from whether the underlying platform is big-endian or little endian. In general, this is a Good Thing.

This article explains how you can "bit-twiddle" values to one byte order or the other:
Code:
result = ((a<<24) | (b<<16) | (c<< 8) | d);// big endian
result = ( a | (b<<8) | (c<<16) | (d<<24) ); // little endian
the article also gives you a helper class, BinaryFile, that might be just what you're looking for:

http://www2.sys-con.com/ITSG/virtual...ton/index.html

'Hope that helps .. PSM
 
Old 12-10-2006, 02:25 AM   #3
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
No, it's not what I wanted
I want an example with binary-writing with setByteOrder method.
Thanks.
 
Old 12-11-2006, 09:16 AM   #4
pasteNoctem
Member
 
Registered: Apr 2006
Location: Athens Greece
Distribution: Kubuntu
Posts: 35

Rep: Reputation: 15
I don't know what the term Endian means... but RandomAccessFiles are like writing in binary..

I don't think I was of any help, but..
 
Old 12-11-2006, 10:38 AM   #5
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
I found what I was looking for:
Code:
public void save (String fn) {
        try {
            ByteBuffer buf = ByteBuffer.allocate (1048576); // 1 MiB
            buf.order (ByteOrder.LITTLE_ENDIAN);
            buf.putInt (123);
            buf.putFloat (3.14159);
            buf.flip ();
            FileChannel out = new FileOutputStream (fn).getChannel ();
            out.write (buf);
            out.close ();
        }
        catch (IOException e) {
            System.out.println ("I/O exception: " + e);
        }
    }
Thanks for help anyway
 
Old 12-11-2006, 11:53 AM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
pasteNoctem -

"Endian" refers to how a given CPU reads bytes in a word: left to right (like Motorola or SPARC processors) or right to left (like Intel x86 processors). Here's a good link:

http://en.wikipedia.org/wiki/Endianness

kornerr -

The reason ByteOrder works is that you happen to be using Java "nio" (instead of, say, Readers, Writers or RandomAccess file types). If your application can easily use nio, great. Otherwise, you'll probably need a more general ("bit twiddling") solution.

IMHO .. pSM
 
Old 12-12-2006, 06:00 AM   #7
pasteNoctem
Member
 
Registered: Apr 2006
Location: Athens Greece
Distribution: Kubuntu
Posts: 35

Rep: Reputation: 15
Thanks for the info and the link..

Ware the RandomAccessFiles of any revelance or help???
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in understanding little endian/big endian machine program indian Programming 6 04-19-2006 02:50 PM
read & write binary file error in redhat xcmore Programming 11 06-17-2005 07:48 AM
big endian little endian? blackzone Linux - Hardware 4 09-23-2004 06:04 AM
What is all this big endian-little endian stuff about? vdemuth Linux - Newbie 1 04-28-2004 02:16 AM
How do you write binary literal constants in C? vasudevadas Programming 5 01-18-2004 07:39 AM


All times are GMT -5. The time now is 02:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration