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)));
        }
      }
   }
2

1 Answer 1

2

Use BigInteger:

String s = "0100001110111111010000111011111101000011101111110100001110111111";
BigInteger bi = new BigInteger(s, 2);
System.out.println(bi.toString(16)); // prints: 43bf43bf43bf43bf
Sign up to request clarification or add additional context in comments.

1 Comment

@Charleemagnee Then please accept the answer by clicking the checkmark, so others can see your question has been answered to your satisfaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.