The Artima Developer Community
Sponsored Link

Java Answers Forum
How to find machine is Big/little endian?

4 replies on 1 page. Most recent reply: Sep 11, 2006 4:08 AM by Ram Das

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Ram Das

Posts: 2
Nickname: ram194
Registered: Sep, 2006

How to find machine is Big/little endian? Posted: Sep 4, 2006 6:02 AM
Reply to this message Reply
Advertisement
Hi,

Can any one tell me that how to find machine on which we are working i.e. Big/Little Endian in using java?

One of my friend told me that in C Programming we can find this by using following code:


#include <stdio.h>
#include <arpa/inet.h>

int main() {
if (htons(1) == 1) puts("big endian");
else puts("little endian");
return 0;
}



Can any one help me in solving this mystery?

Cheers,
Ram


Colin Diesh

Posts: 1
Nickname: tonto
Registered: Sep, 2006

Re: How to find machine is Big/little endian? Posted: Sep 4, 2006 11:05 PM
Reply to this message Reply
The JVM is big endian format. Do you really need to know the endianness of the particular machine?

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: How to find machine is Big/little endian? Posted: Sep 6, 2006 3:30 PM
Reply to this message Reply
It matters if one is reading a binary file created by a non-Java program. For example, I had to read in some MATLAB files that were wrong-endian. In my case it was easy to notice because none of the floats or doubles printed by the Java program matched the numbers I expected.

I think the simplest way to test is to just read in a few items from the file and print out the values to see if they match what you expect. If need be, you can Google to find several examples of code to swap the bytes. Good luck.

Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: How to find machine is Big/little endian? Posted: Sep 7, 2006 8:06 AM
Reply to this message Reply
Try boolean isLittleEndian = "little".equals(System.getProperty("sun.cpu.endian")) if you run a Sun VM. Don't know how reliable this is, it is not portable.

Ram Das

Posts: 2
Nickname: ram194
Registered: Sep, 2006

Re: How to find machine is Big/little endian? Posted: Sep 11, 2006 4:08 AM
Reply to this message Reply
I got answer from Sun Java Forum:

Here is the answer:

import java.nio.ByteOrder;

public class Endian {
public static void main(String argv[]) {
ByteOrder b = ByteOrder.nativeOrder();
if (b.equals(ByteOrder.BIG_ENDIAN)) {
System.out.println("Big-endian");
} else {
System.out.println("Little-endian");
}
}
}

Flat View: This topic has 4 replies on 1 page
Topic: Eclipse 3.1 with SVN Code Version System Previous Topic   Next Topic Topic: j-Interop: Open Source, Non Native access to DCOM

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use