ByteOrder写入buffer时字节的顺序

Home / Android MrLee 2015-1-30 5021

-java默认是big-endian(大端序)
*API
---2个内置的ByteOrder
ByteOrder.BIG_ENDIAN和ByteOrder.LITTLE_ENDIAN
---ByteOrder.nativeOrder()
返回本地jvm运行的硬件的字节顺序.使用和硬件一致的字节顺序可能使buffer更加有效.
---ByteOrder.toString()
返回ByteOrder的名字,BIG_ENDIAN或LITTLE_ENDIAN
*示例
		ByteBuffer buf = ByteBuffer.allocate(10);
		System.out.println(buf.order().toString());
		buf.putInt(599999999);
		buf.flip();
		for (int i = 0; i < buf.limit(); i++)
			System.out.print((buf.get() & 0xFF) + ",");
		buf.clear();
		buf.order(ByteOrder.LITTLE_ENDIAN);
		System.out.println();
		System.out.println(buf.order().toString());
		buf.putInt(599999999);
		buf.flip();
		for (int i = 0; i < buf.limit(); i++)
			System.out.print((buf.get() & 0xFF) + ",");
输出结果: BIG_ENDIAN 35,195,69,255, LITTLE_ENDIAN 255,69,195,35,

本文链接:https://www.it72.com/959.htm

推荐阅读
最新回复 (0)
返回