【问题标题】:I want to convert a binary string to corresponding hex string.But when the input binary string long then NumberFormatException is shown.How to fix it?我想将二进制字符串转换为相应的十六进制字符串。但是当输入的二进制字符串很长时,会显示 NumberFormatException。如何解决?
【发布时间】:2016-03-31 03:25:34
【问题描述】:

我的代码在这里。实际上我想将诸如“0100001110111111”之类的二进制字符串转换为相应的十六进制格式。该代码对于小输入字符串工作正常,但对于长输入字符串会显示 NumberFormatException 并且它不工作。

    public class Test 
    {
      public static void main(String[] args) 
      {
        for (int i = 0; i < args.length; i++) 
        {
           System.out.println("The value of " + args[i] + " is " +
           Integer.toHexString(Integer.parseInt(args[i], 2)));
        }
      }
   }

【问题讨论】:

标签: java string hex


【解决方案1】:

使用BigInteger:

String s = "0100001110111111010000111011111101000011101111110100001110111111";
BigInteger bi = new BigInteger(s, 2);
System.out.println(bi.toString(16)); // prints: 43bf43bf43bf43bf

【讨论】:

  • @Charleemagnee 那么请点击复选标记接受答案,以便其他人可以看到您的问题得到了满意的回答。
猜你喜欢
  • 2019-08-23
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 2017-03-22
  • 2017-02-23
  • 2014-08-03
  • 2015-06-21
相关资源
最近更新 更多