convert hexadecimal number string to double-precision number in java

☆樱花仙子☆ 提交于 2020-01-04 09:11:29

问题


how can we convert hexadecimal number string to double-precision number in java ?

in matlab it's simple :

>> hex2num('c0399999a0000000')

ans =

  -25.6000

but could I do the same things in java also ?

I tried parseInt() but this number is not integer.


回答1:


I think you want Double.longBitsToDouble, like this:

public class Test {
    public static void main(String[] args) {
        String hex = "c0399999a0000000";
        long longHex = parseUnsignedHex(hex);
        double d = Double.longBitsToDouble(longHex);
        System.out.println(d);
    }

    public static long parseUnsignedHex(String text) {
        if (text.length() == 16) {
            return (parseUnsignedHex(text.substring(0, 1)) << 60)
                    | parseUnsignedHex(text.substring(1));
        }
        return Long.parseLong(text, 16);
    }
}

(The fact that long is signed in Java makes this more awkward than you'd really want, but hey...)




回答2:


First make a long, and then call longBitsToDouble



来源:https://stackoverflow.com/questions/10708362/convert-hexadecimal-number-string-to-double-precision-number-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!