2

I have looked at a lot of post here on stackoverflow concerning this problem, I have found partial solutions but so far I have yet to find a solution that works for me. new BigDecimal("3324679375210329505").toString(2); Seems to work best for me (from: Convert a large 2^63 decimal to binary) but I do need leading and trailing zeros. Is there anyway I can convert a large (bigger than a long) hex to a binary (String) representation?

Thanks in advance.

Community
  • 1
  • 1
Elian ten Holder
  • 262
  • 1
  • 3
  • 15

2 Answers2

3

The BigInteger class handles arbitrarily large numbers.

Trailing zeroes are allready handled. To handle leading zeroes, simply add a "1" at the front, which ensures a leading "1" bit, then strip it back off again:

String bits = new BigInteger("1" + hex, 16).toStting(2).substring(1);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • @jlordo It does: `BigInteger(String str, int radix)` – Valeri Atamaniouk Apr 20 '13 at 20:52
  • @ValeriAtamaniouk: Yes, you are right, I was looking at BgDecimal. My mistake. – jlordo Apr 20 '13 at 20:59
  • But I believe this solution get rids of leading zeros, and for proper conversion to base64(my end goal) I need leading and trailing zeros. – Elian ten Holder Apr 20 '13 at 21:02
  • @Jordo See edited answer. (I realised the leading zeroes problem, but was busy for a few minutes) – Bohemian Apr 20 '13 at 21:10
  • 1
    @EliantenHolder as you can see, this answer does it in one simple line. – Bohemian Apr 20 '13 at 21:17
  • @Bohemian: upvoted. Didn't think of that nice trick. Can't delete my answer, as long as it's the accepted one. – jlordo Apr 20 '13 at 23:04
  • Very nice code this was the kind of 'complex' code I was referring to in my comment on @jlordo code. I'm re-selecting the accepted answer but you still have my upvote for the fastest and most simplistic answer! jlordo please don't delete your answer I think it demonstrates how you can do it 'by hand'. – Elian ten Holder Apr 20 '13 at 23:19
2

You need leading zeroes? I don't know of a built-in function, but you can easily implement it yourself:

public static String hex2binary(String hex) {
    StringBuilder result = new StringBuilder(hex.length() * 4);
    for (char c : hex.toUpperCase().toCharArray()) {
        switch (c) {
        case '0': result.append("0000"); break;
        case '1': result.append("0001"); break;
        case '2': result.append("0010"); break;
        case '3': result.append("0011"); break;
        case '4': result.append("0100"); break;
        case '5': result.append("0101"); break;
        case '6': result.append("0110"); break;
        case '7': result.append("0111"); break;
        case '8': result.append("1000"); break;
        case '9': result.append("1001"); break;
        case 'A': result.append("1010"); break;
        case 'B': result.append("1011"); break;
        case 'C': result.append("1100"); break;
        case 'D': result.append("1101"); break;
        case 'E': result.append("1110"); break;
        case 'F': result.append("1111"); break;
        default: throw new IllegalArgumentException("Invalid hex: '" + hex + "'");
        }
    }
    return result.toString();
}
jlordo
  • 37,490
  • 6
  • 58
  • 83
  • Nice one. I suspect handling lower-case characters in switch might be faster, then calling `toUpperCase`... – Valeri Atamaniouk Apr 20 '13 at 21:03
  • @ValeriAtamaniouk: You mean extra cases? I was too lazy to type ;) No matter how long the input is, this won't be the bottleneck of the application... – jlordo Apr 20 '13 at 21:03
  • Great that is the answer I was looking for, very simplistic yet very useful :) – Elian ten Holder Apr 20 '13 at 21:06
  • @EliantenHolder: Then why didn't you write it yourself? ;) – jlordo Apr 20 '13 at 21:07
  • @jlordo It's quite late and tbh I was thinking of a solution way to complex for the problem. I wish I came up with your answer by my self would have been faster :) Thanks for your fast answer you've helped me along :) – Elian ten Holder Apr 20 '13 at 23:16