java hex to double_java – Double to Hex String and Back

Java中将double转换为十六进制字符串相当简单.但是我该怎么做呢?我的代码在下面,我已经注意到抛出了NumberFormatException(大约2 / 3rds).

public class HexToDoubleTest {

public static void main( String args[] ) {

// This is the starting double value

double doubleInput = -9.156013e-002;

// Convert the starting value to the equivalent value in a long

long doubleAsLong = Double.doubleToRawLongBits( doubleInput );

// Convert the long to a String

String doubleAsString = Long.toHexString( doubleAsLong );

// Print the String

System.out.println( doubleAsString );

// Attempt to parse the string back as a long

// !!! This fails with a NumberFormatException !!!

long doubleAsLongReverse = Long.parseLong( doubleAsString, 16 );

// Convert the long back into the original double

double doubleOutput = Double.longBitsToDouble( doubleAsLongReverse );

// Confirm that the values match

assert( doubleInput == doubleOutput );

}

}

使用Double.valueOf以相同的方式失败.

编辑:我已经在网上做了一些搜索,发现了一些非常不优雅的解决方案.例如:使用BigInteger似乎有点矫枉过正.必须有更好的方法!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值