Java代码测试大端小端
运行结果:LITTLE_ENDIAN
public static void main(String[] args)
{
try
{
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe unsafe=(Unsafe)f.get(null);
ByteOrder byteOrder=null;
long a = unsafe.allocateMemory(8);
try
{
unsafe.putLong(a, 0x0102030405060708L);
byte b = unsafe.getByte(a);
switch (b)
{
case 0x01:
byteOrder = ByteOrder.BIG_ENDIAN;
break;
case 0x08:
byteOrder = ByteOrder.LITTLE_ENDIAN;
break;
default:
byteOrder = null;
}
}
catch (Exception ee)
{
ee.printStackTrace();
}
finally
{
unsafe.freeMemory(a);
}
System.out.println(byteOrder);
}
catch (Exception e)
{
e.printStackTrace();
}
}
全部评论