输入字符串中的 NumberFormatException

发布于 2024-12-08 09:35:20 字数 541 浏览 0 评论 0原文

请帮我解决这个异常:-

String strBinary="100000000000000001000000000000000000000000000000000000000000000000000000";
        System.out.println("length is " + strBinary.length()); 
        long intParse=Long.parseLong(strBinary, 2);
        System.out.println("int parsed is " +   intParse); 
        String hexString=Long.toHexString(intParse);
          System.out.println(hexString);

使用 Long.parseLong 解析时输出为 72 以及 NumberFormatException。 但直到昨天,这个输入也运行得非常好。 和长度有关系吗... 我实际上正在尝试将字符串转换为其等效的十六进制值。

请帮忙....

Please help me solve this exception:-

String strBinary="100000000000000001000000000000000000000000000000000000000000000000000000";
        System.out.println("length is " + strBinary.length()); 
        long intParse=Long.parseLong(strBinary, 2);
        System.out.println("int parsed is " +   intParse); 
        String hexString=Long.toHexString(intParse);
          System.out.println(hexString);

Output is 72 along with NumberFormatException while parsing using Long.parseLong..
But till yesterday it was running absolutely fine for this input also..
does it have anything to do with the length...
I am actuallly trying to convert String into its equivalent Hex value.

Please help....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-12-15 09:35:20

long 可以保存 64 位数据。 long 可以表示的最大值是 9223372036854775807(或 263-1)。您尝试解析的字符串比该字符串大得多

可能可以使用 BigInteger 类,它可以处理任意大小的整数值(当然,有效地受内存限制)。

A long can hold 64 bit of data. The biggest value a long can represent is 9223372036854775807 (or 263-1). The string you try to parse is a lot larger than that.

You might be able to go somewhere by using the BigInteger class, which can handle arbitrary-sized integer values (effectively restricted by memory, of course).

梦罢 2024-12-15 09:35:20

长对于您的目的来说很小。您可能想像这样使用 BigInteger 对象

String strBinary="100000000000000001000000000000000000000000000000000000000000000000000000";
BigInteger bigInteger = new BigInteger(strBinary, 2);
System.out.println(bigInteger.longValue()); //This would give you the long value
System.out.println(bigInteger.toString(16)); //This would give you the hex string

Long is small for your purpose. You might want to use BigInteger object like this

String strBinary="100000000000000001000000000000000000000000000000000000000000000000000000";
BigInteger bigInteger = new BigInteger(strBinary, 2);
System.out.println(bigInteger.longValue()); //This would give you the long value
System.out.println(bigInteger.toString(16)); //This would give you the hex string
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文