在Java中将十六进制数字字符串转换为双精度数字 [英] convert hexadecimal number string to double-precision number in java
本文介绍了在Java中将十六进制数字字符串转换为双精度数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Java中将十六进制数字字符串转换为双精度数字?
how can we convert hexadecimal number string to double-precision number in java ?
在matlab中很简单:
in matlab it's simple :
>> hex2num('c0399999a0000000')
ans =
-25.6000
但是我也可以在java中做同样的事情吗?
but could I do the same things in java also ?
我尝试了parseInt(),但是这个数字不是整数.
I tried parseInt() but this number is not integer.
推荐答案
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);
}
}
(long
用Java签名的事实使此操作比您真正想要的更加尴尬,但是嘿...)
(The fact that long
is signed in Java makes this more awkward than you'd really want, but hey...)
这篇关于在Java中将十六进制数字字符串转换为双精度数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文