JAVA,无法对字节重新排序
我是Java编程新手,我尝试编写此函数(调试)作为学习练习,但它不起作用,我不明白为什么: 功能JAVA,无法对字节重新排序,java,endianness,Java,Endianness,我是Java编程新手,我尝试编写此函数(调试)作为学习练习,但它不起作用,我不明白为什么: 功能 public static void ByteReOrder(){ byte[] byte_array_uid = new byte[] { (byte)0x3A, (byte)0x2B, (byte)0x07 }; out.println("UID ("+ByteOrder.nativeOrder()+"): " + HumanReprByteToHex(byte_array_uid));
public static void ByteReOrder(){
byte[] byte_array_uid = new byte[] { (byte)0x3A, (byte)0x2B, (byte)0x07 };
out.println("UID ("+ByteOrder.nativeOrder()+"): " + HumanReprByteToHex(byte_array_uid));
if(Byte.nativeOrder() == ByteOrder.LITTLE_ENDIAN){
ByteBuffer byte_buffer_uid = ByteBuffer.wrap(byte_array_uid);
out.println("Default ByteOrder is LITTLE_ENDIAN, change for BIG_ENDIAN ...");
byte_buffer_uid.order(ByteOrder.BIG_ENDIAN);
byte[] byte_array_from_byte_buffer_uid = new byte[byte_buffer_uid.remaining()];
byte_buffer_uid.get(byte_array_from_byte_buffer_uid);
out.println("UID ("+byte_buffer_uid.order()+") : " + HumanReprByteToHex(byte_array_from_byte_buffer_uid));
}else{
out.println("UID (LITTLE_ENDIAN): " + HumanReprByteToHex(byte_array_uid));
}
}
输出
UID (LITTLE_ENDIAN): 0x3A 0x2B 0x07
Default ByteOrder is LITTLE_ENDIAN, change for BIG_ENDIAN ...
UID (BIG_ENDIAN) : 0x3A 0x2B 0x07
0x07 0x2B 0x3A
结果总是以小尾数形式出现,但字节\u缓冲区\u uid.order()似乎是正确的,我阻止了
尝试结果
UID (LITTLE_ENDIAN): 0x3A 0x2B 0x07
Default ByteOrder is LITTLE_ENDIAN, change for BIG_ENDIAN ...
UID (BIG_ENDIAN) : 0x3A 0x2B 0x07
0x07 0x2B 0x3A
提前谢谢。来自java文档
此类定义了用于读取和写入除boolean之外的所有其他基元类型的值的方法。根据缓冲区的当前字节顺序,将原语值转换为(或从)字节序列,可以通过顺序方法检索和修改。特定字节顺序由ByteOrder类的实例表示。字节缓冲区的初始顺序总是BIG_ENDIAN
意味着ByteBuffer(和Java)的默认值总是BIG_ENDIAN。这将影响基元类型(short、int、long、float和double)的getter和setter方法
仅调用get(array)
或array()
将获取字节数组的备份副本,它不会反映任何顺序
ByteBuffer bb = new ByteBuffer();
bb.order(ByteOrder.BIG_ENDIAN);
bb.putFloat(1234.123f);
byte[] bytesOne = bb.array();
bb.clear();
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putFloat(1234.123f);
byte[] bytesTwo = bb.array();
// compare betwee bytesOne and bytesTwo to see different byte ordering.
尝试在“放入”一些原语值之后、放入不同顺序之前和之后比较字节数组
ByteBuffer bb = new ByteBuffer();
bb.order(ByteOrder.BIG_ENDIAN);
bb.putFloat(1234.123f);
byte[] bytesOne = bb.array();
bb.clear();
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putFloat(1234.123f);
byte[] bytesTwo = bb.array();
// compare betwee bytesOne and bytesTwo to see different byte ordering.
默认的
byteOrder()
已经是BIG_ENDIAN,因此设置它甚至不会更改ByteBuffer本身。即使要更改字节顺序,这也只是关于如何读取/写入多字节值的设置。它不会改变单个字节的顺序,只会改变在执行多字节操作时读取或写入字节的方式
public static void main(String[] args) {
byte[] bytes = {1, 2, 3, 4, 5, 6, 7, 8};
ByteBuffer bb = ByteBuffer.wrap(bytes);
print(bb.order(ByteOrder.BIG_ENDIAN));
print(bb.order(ByteOrder.LITTLE_ENDIAN));
}
private static void print(ByteBuffer bb) {
System.out.println("With " + bb.order() + " byte order");
for (int i = 0; i < bb.capacity(); i++)
System.out.println("byte " + i + ": " + bb.get(i));
for (int i = 0; i < bb.capacity() - 2; i++)
System.out.println("short " + i + ": " + Integer.toHexString(bb.getShort(i)));
for (int i = 0; i < bb.capacity() - 3; i++)
System.out.println("int " + i + ": " + Integer.toHexString(bb.getInt(i)));
}
当您将int、long或float写入ByteBuffer时,它并不知道您编写了这些类型,只知道您编写了字节。这意味着当您更改字节顺序设置时,它无法更改基础数据,因为它不知道前8个字节是双字节、双整数、4短字节还是8字节
这样做的唯一方法是使用原始类型读取数据。只有在这一点上,代码才会改变字节的顺序
顺便说一句,在某些机器上,本机顺序是BIG_ENDIAN。请尝试使用偶数字节。这可能有助于我更好地理解,谢谢你提供的信息。是的,默认情况下会出现BIG_ENDIAN。@中止的安全性我的意思是说“本机顺序是BIG_ENDIAN”@中止的安全性我添加了一个可能会有所帮助的示例。我已经看到并正在阅读您的示例,我不知道如何感谢您@如果你愿意,你可以接受我的回答;)