How to convert an hexadecimal string to a double?

前端 未结 1 1108
一整个雨季
一整个雨季 2021-01-16 22:56

I\'m getting the hexadecimal values of range 0x0000 to 0x01c2 from BLE to my phone a as String. In order to plot it in a graph, I have to convert it into

相关标签:
1条回答
  • 2021-01-16 23:47

    Your value isn't a hex representation of an IEEE-754 floating point value - it's just an integer. So just parse it as an integer, after removing the leading "0x" prefix:

    public class Test {
        public static void main(String[] args) {
            String text = "0x009a";
    
            // Remove the leading "0x". You may want to add validation
            // that the string really does start with 0x
            String textWithoutPrefix = text.substring(2);
            short value = Short.parseShort(textWithoutPrefix, 16);
            System.out.println(value);
        }
    }
    

    If you really need a double elsewhere, you can just implicitly convert:

    short value = ...;
    double valueAsDouble = value;
    

    ... but I'd try to avoid doing so unless you really need to.

    0 讨论(0)
提交回复
热议问题