1

I'm trying to convert this binary String that contains 64bits to Hexadecimal with this code:

String mm = "1000010111101000000100110101010000001111000010101011010000000101";
String v = new BigInteger(mm, 2).toString(16);
v=String.format("%64x", v);

but it gives me this exception:

Exception in thread "main" java.util.IllegalFormatConversionException: x != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
    at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.lang.String.format(Unknown Source)
    at test.main(test.java:332)

what is getting wrong and why?

3
  • 1
    That looks like a binary string rather than a hex string. Why the 16 in the BigInteger constructor? Shouldn't it be 2? Commented Mar 10, 2017 at 1:27
  • @JohnColeman Sorry, you are right, I had edit it and forgot to change when I have post this question. Thank u. Commented Mar 10, 2017 at 1:29
  • Your String.format("%64x", v); uses a type character of x which says to treat the argument v as an unsigned int and format it in hexadecimal — but your v is a String already. Commented Mar 10, 2017 at 1:35

1 Answer 1

3

change this:

v=String.format("%64x", v);

to this:

v=String.format("%s", v);

Also you need this:

String v = new BigInteger(mm, 2).toString(16);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the solution – it was great. :)
This just appends 64x onto the end of 85e813540f0ab405

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.