Translating a String containing a binary value to Hex

前端 未结 2 496
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 04:54

I am trying to translate a String that contains a binary value (e.g. 000010001010011) to it\'s Hex value.(453)

I\'ve been trying several options, but mostly I get a

相关标签:
2条回答
  • 2020-12-16 05:32

    Try using Integer.parseInt(binOutput, 2) instead of Integer.parseInt(binOutput)

    0 讨论(0)
  • 2020-12-16 05:44

    Ted Hopp beat me to it, but here goes anyway:

    jcomeau@intrepid:/tmp$ cat test.java; java test 000010001010011
    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)));
      }
     }
    }
    The value of 000010001010011 is 453
    
    0 讨论(0)
提交回复
热议问题