![](/img/trans.png)
[英]How to use Byte Buffer to serialize a Byte Array to start following Big Endian format?
[英]Byte ordering in byte buffer for big endian
我知道小字節序和大字節序在網絡上被解釋過很多次,但我仍然感到困惑。
如果您將字節緩沖區設置為使用大端字節序。 你有:
short value = 4660; // corresponds to 0x1234
當您這樣做時:
bb.putShort( 16, value );
字節0x12是位於索引16處,而0x34是位於索引17。還是0x34是位於索引16處,而0x12是位於索引17? 我對在線描述感到困惑。
謝謝!
以0x12345678
為例。
一種查看方式:
Endianess | Least Significant Byte | Most Significant Byte | In Memory
----------|------------------------|------------------------|-----------
Big | In The Highest Address | In The Lowest Address | 0x12345678
----------|------------------------|------------------------|-----------
Little | In The Lowest Address | In The Highest Address | 0x78563412
另一種查看方式:
Endianess | In The Lowest Address | In The Highest Address | In Memory
----------|------------------------|------------------------|-----------
Big | Most Significant Byte | Least Significant Byte | 0x12345678
----------|------------------------|------------------------|-----------
Little | Least Significant Byte | Most Significant Byte | 0x78563412
如果您正在尋找一種記住這一點的方法,請注意每種方法都有其自身的意義:
您可以通過編寫6(!)行代碼來解決這一問題。
ByteBuffer allocate = ByteBuffer.allocate(2);
allocate.order(ByteOrder.BIG_ENDIAN);
allocate.putShort((short) 0x1234);
allocate.rewind();
System.out.println(Integer.toHexString(allocate.get()));
System.out.println(Integer.toHexString(allocate.get()));
提示:“ Big End” -ian在最后一個地址處有最后一個字節。 因此,內存正是您將其讀取為0x12 0x34
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.