Java big-endian的字节缓冲区中的字节排序

Java big-endian的字节缓冲区中的字节排序,java,endianness,bytebuffer,Java,Endianness,Bytebuffer,我知道little endian和big endian在网上被解释了无数次,但我还是很困惑 如果有一个字节缓冲区设置为使用big-endian。你有: short value = 4660; // corresponds to 0x1234 当您这样做时: bb.putShort( 16, value ); 字节0x12将位于索引16,字节0x34将位于索引17。或者0x34在索引16处,0x12在索引17处?我被网上的描述弄糊涂了 谢谢 以0x12345678为例 一种看待它的方式: En

我知道little endian和big endian在网上被解释了无数次,但我还是很困惑

如果有一个字节缓冲区设置为使用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
另一种看待它的方式:

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

如果您正在寻找一种记忆方法,请注意,每种方法都有其自身的意义:

  • Big-Endian,因为它类似于我们写数字的方式(从最高数字开始)
  • 小尾端,因为最低有效字节存储在最低地址

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
另一种看待它的方式:

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

如果您正在寻找一种记忆方法,请注意,每种方法都有其自身的意义:

  • Big-Endian,因为它类似于我们写数字的方式(从最高数字开始)
  • 小尾端,因为最低有效字节存储在最低地址

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
另一种看待它的方式:

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

如果您正在寻找一种记忆方法,请注意,每种方法都有其自身的意义:

  • Big-Endian,因为它类似于我们写数字的方式(从最高数字开始)
  • 小尾端,因为最低有效字节存储在最低地址

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
另一种看待它的方式:

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

如果您正在寻找一种记忆方法,请注意,每种方法都有其自身的意义:

  • Big-Endian,因为它类似于我们写数字的方式(从最高数字开始)
  • 小尾端,因为最低有效字节存储在最低地址

您可以通过编写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()));

提示:“大端”-ian在最后一个地址有最后一个字节。因此,内存就是您将其读取为
0x12 0x34

的方式。您可以通过编写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()));

提示:“大端”-ian在最后一个地址有最后一个字节。因此,内存就是您将其读取为
0x12 0x34

的方式。您可以通过编写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()));

提示:“大端”-ian在最后一个地址有最后一个字节。因此,内存就是您将其读取为
0x12 0x34

的方式。您可以通过编写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()));

提示:“大端”-ian在最后一个地址有最后一个字节。因此,内存正是您将其读取为
0x12 0x34

的方式,您为什么不编写一个简单的测试应用程序来自己解决这个问题呢?“Big-endian”意味着数字的大端位于最低的地址值。你肯定已经读过1000遍了?这会使你的问题重复。你为什么不自己编写一个简单的测试应用程序来解决这个问题呢?“Big-endian”意味着数字的大端位于最低的地址值。你肯定已经读过1000遍了?这会使你的问题重复。你为什么不自己编写一个简单的测试应用程序来解决这个问题呢?“Big-endian”意味着数字的大端位于最低的地址值。你肯定已经读过1000遍了?这会使你的问题重复。你为什么不自己编写一个简单的测试应用程序来解决这个问题呢?“Big-endian”意味着数字的大端位于最低的地址值。你肯定已经读过1000遍了?这使你的问题重复起来。