tags:

views:

356

answers:

3

So I have a set of base digits like "BCDFGHJKLMNPQRSTVWXZ34679"

how do I convert a value say "D6CN96W6WT" to binary string in Java?

+2  A: 

Off the top of my head, for base-25 strings.

Integer.toString(Integer.valueof(base25str, 25), 2)

Its a little unclear from your question whether you're talking about actual 0-9-Z bases, or a number encoding with an arbitrary list of symbols. I'm assuming the first, if its the later then you're out of luck on built-ins.

Kevin Montrose
For long values, you can use `Long` instead of `Integer`
notnoop
unfortunately, it was a random list of symbols
+6  A: 

This should work (assuming 0,1 for you binary digits):

// your arbitrary digits
private static final String DIGITS = "BCDFGHJKLMNPQRSTVWXZ34679";

public String base25ToBinary(String base25Number) {
    long value = 0;
    char[] base25Digits = base25Number.toCharArray();

    for (char digit : base25Digits) {
        value = value * 25 + DIGITS.indexOf(digit);
    }

    return Long.toString(value, 2);
}
Jack Leow
A: 

@jack

sorry bro ur program doesn't run i think it should be having wrapper classes at the base of it...

Regards, Dhaval.

Dhaval