当我尝试在文件中写入具有如下值的二进制文件时:
public static main(String[] args){
ByteBuffer output = ByteBuffer.allocate(80);
output.order(ByteOrder.BIG_ENDIAN);
output.putDouble(545.5);
appendByteArrayInFile("c:/myPath/", "test.bin", output.array());
}
private static void appendByteArrayInFile(String exportDirectory, String fileName, byte[] toAppendInFile) {
if (toAppendInFile != null) {
File targetExport = createPathAndFile(exportDirectory + fileName);
try (FileOutputStream output = new FileOutputStream(targetExport, true)) {
output.write(toAppendInFile);
} catch (Exception e) {
// no
}
}
}
private static File createPathAndFile(String path) {
File targetExport = new File(path);
targetExport.getParentFile().mkdirs();
return targetExport;
}
问题在于,当我查看生成的文件时,似乎双端以小端方式推出,当我将ByteOrder切换为little-endian时,double是用big-endian编写的... 但是当我输入int时,字节顺序是正确的。
BigEndian中输出为double:
01000000 10000001 00001100 00000000 00000000 00000000 00000000 00000000
在littleEndian中输出double:
00000000 00000000 00000000 00000000 00000000 00001100 10000001 01000000
在bigEndian中使用int输出:
00000000 00000000 00000010 00100001
答案 0 :(得分:2)
据我了解,IEEE浮点数中的有效数字根本不是“endian”,因为它不是一个独立的数值。如果有效数字表示为基数2,则它是一个二进制数字序列,它将跟随小数点。 (在小数点的左侧,始终假定为“1”。)有效数字表示为0001 00001100 00000000 00000000 00000000 00000000 00000000
,表示实际有效数为1.00010000110 ... 2 。那么,实际值是1.0001000011 2 ×2 9 ,即545.5。