I use the following code to convert a hexadecimal String to a floating point String :
private static String removeScientificNotation(float value) { return new BigDecimal(Float.toString(value)).toPlainString(); } public static String hexadecimalToFloatingPoint(String hexadecimal) { Long longBits = Long.parseLong(hexadecimal, 16); Float floatValue = Float.intBitsToFloat(longBits.intValue()); return removeScientificNotation(floatValue); }
To test this, I wrote the following JUnit test:
public class TestConversions { @Test public void testConversions() { String floatValue = Conversions.hexadecimalToFloatingPoint("40000000"); Assert.assertEquals(floatValue, "2.0"); floatValue = Conversions.hexadecimalToFloatingPoint("50000000"); Assert.assertEquals(floatValue, "8589934592"); floatValue = Conversions.hexadecimalToFloatingPoint("C0000000"); Assert.assertEquals(floatValue, "-2.0"); } }
However, the second statement does not hold. According to various online converters, like this one , 50000000 should be converted to 8589934592 , but Java returns 8589934600 .
org.junit.ComparisonFailure: Expected :8589934600 Actual :8589934592
What is the correct result now? If Java is incorrect, then how to fix it?
source share