我正在尝试将字符串转换为双精度。但我得到了数字格式异常。给出异常的代码是这样的:
double B = Double.parseDouble(s);
我将 s 值从服务器带到我的应用程序。它是这样的:
fd485b154a0427fa75c0428cdafac71f
有什么解决办法吗?
我正在尝试将字符串转换为双精度。但我得到了数字格式异常。给出异常的代码是这样的:
double B = Double.parseDouble(s);
我将 s 值从服务器带到我的应用程序。它是这样的:
fd485b154a0427fa75c0428cdafac71f
有什么解决办法吗?
1. Double.parseDouble()
仅适用于Double
Numbers,不适用于non-Numbers
。
2. fd485b154a0427fa75c0428cdafac71f
是没有号码......
试试这个:(我假设字符串是其中的 0-9 和 af 的 HEX coz)
double doubleVal = new BigInteger("fd485b154a0427fa75c0428cdafac71f", 16).longValue();
long longVal = new BigInteger("fd485b154a0427fa75c0428cdafac71f", 16).longValue();
System.out.println(doubleVal);
System.out.println(longVal);
这给出了:
8.4848548707027374E18
8484854870702737183
Double.parseDouble(s)
s
如果是数字,则做得很好。显然,您收到的是以十六进制为基数的数字。对?
这很简单:
double doubleAsLongReverse = new BigInteger(doubleAsString, 16).longValue();
double B = Double.parseDouble(s);
仅适用于数字,不适用于此类 fd485b154a0427fa75c0428cdafac71f 值示例
String s = "123";
double B = Double.parseDouble(s);
System.out.println(B);
Hex
在 Apache 的 Commons Codec 中使用。
您可以使用 ByteBuffer 类http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html 您可以从字符串 s.getBytes() 中放入字节,然后调用 getDouble()
希望能帮助到你。
我会用
double doubleVal = new BigInteger("fd485b154a0427fa75c0428cdafac71f", 16).doubleValue();
System.out.println(doubleVal);
印刷
3.366703756933705E38
如果您使用 .longValue() 您将由于溢出而切断数字的开头。