0

My code is here.Actually I want to convert a binary string such as "0100001110111111" into corresponding hex format.The code is working fine for small input strings but for long input string NumberFormatException is shown and it is not working.

    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)));
        }
      }
   }
  • [How can I convert a string into hex in Java] http://stackoverflow.com/questions/18946597/how-can-i-convert-a-string-into-hex-in-java – Mizuki Mar 31 '16 at 03:45
  • [Converting A String To Hexadecimal In Java] (http://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java) – Mizuki Mar 31 '16 at 03:50

1 Answers1

2

Use BigInteger:

String s = "0100001110111111010000111011111101000011101111110100001110111111";
BigInteger bi = new BigInteger(s, 2);
System.out.println(bi.toString(16)); // prints: 43bf43bf43bf43bf
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    @Charleemagnee Then please accept the answer by clicking the checkmark, so others can see your question has been answered to your satisfaction. – Andreas Apr 06 '16 at 05:40