java 十六进制 long,如何在java中将十六进制字符串转换为long?

I want to convert a hex string to long in java.

I have tried with general conversion.

String s = "4d0d08ada45f9dde1e99cad9";

long l = Long.valueOf(s).longValue();

System.out.println(l);

String ls = Long.toString(l);

But I am getting this error message:

java.lang.NumberFormatException: For input string: "4d0d08ada45f9dde1e99cad9"

Is there any way to convert String to long in java? Or am i trying which is not really possible!!

Thanks!

解决方案

Long.decode(str) accepts a variety of formats:

Accepts decimal, hexadecimal, and octal

numbers given by the following

grammar:

DecodableString:

Signopt DecimalNumeral

Signopt 0x HexDigits

Signopt 0X HexDigits

Signopt # HexDigits

Signopt 0 OctalDigits

Sign:

-

But in your case that won't help, your String is beyond the scope of what long can hold. You need a BigInteger:

String s = "4d0d08ada45f9dde1e99cad9";

BigInteger bi = new BigInteger(s, 16);

System.out.println(bi);

Output:

23846102773961507302322850521

For Comparison, here's Long.MAX_VALUE:

9223372036854775807

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值