Java ByteBuffer BigEndian Double

When I try to write binaries to a file with a value like this:

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; } 

The fact is that when I look at the generated file, it seems that the double is placed in a little endian style, and when I switch ByteOrder to little-endian, double is written in big-endian ... But when I put the int, the validity is correct.

BigEndian double exit:

 01000000 10000001 00001100 00000000 00000000 00000000 00000000 00000000 

Double exit in littleEndian:

 00000000 00000000 00000000 00000000 00000000 00001100 10000001 01000000 

Exit with int in bigEndian:

 00000000 00000000 00000010 00100001 
+5
source share
1 answer

As far as I understand, the value in the number of IEEE floating-point numbers is not "final" at all, since it is not a separate numerical value. Its sequence of binary digits, which will follow the decimal point, if the value was expressed as a base number 2. (To the left of the decimal point is always meant "1."). The value is represented as 0001 00001100 00000000 00000000 00000000 00000000 00000000 , which means that the actual value is 1.00010000110 ... 2 . Then the actual value is 1.0001000011 2 × 2 9 which is 545.5.

+2
source

Source: https://habr.com/ru/post/1268103/


All Articles