user93796 user93796 - 21 days ago 11
Java Question

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.

Nim Nim
Answer

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");
}
Comments