Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find endianness of system in java

I found out that algorithm (int C) for checkink if machine is bigindian or littleindian is

int is_big_endian(void)
{
    union {
        uint32_t i;
        char c[4];
    } bint = {0x01020304};

    return bint.c[0] == 1; 
}

How can i find such thing in *java?*I dont want to use inbuilt libs as this is a interview question.I want to find it out in java.

like image 698
user93796 Avatar asked Feb 24 '12 13:02

user93796


1 Answers

I take no credit for this, however you can try:

import java.nio.ByteOrder;

if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) {
  System.out.println("Big-endian");
} else {
  System.out.println("Little-endian");
}
like image 92
Nim Avatar answered Nov 14 '22 21:11

Nim