1
\$\begingroup\$

I wrote a base conversion program. This base conversion should also work with base 1 (unary) where 000 is 3 and 0000000 is 7 and so on. It is only for non-negative numbers.

public class BaseConversion{
    private final static String alpha="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static String ltob(long v, int base){
        if(base>alpha.length()){
            throw new IllegalArgumentException("base must not be greater than "+alpha.length());
        }
        if(base<=0){
            throw new IllegalArgumentException("base must not be lower than 0");
        }
        if(v<0){
            throw new IllegalArgumentException("input must not be lower than 0");
        }
        if(base==1){
            String ret="";
            while(v-->=0){
               ret+="0";
            }
            return ret;
        }
        String ret="";
        do{
            int x=(int)(v%((long)base));
            v/=base;
            ret=alpha.charAt(x)+ret;
        }while(v!=0);
        return ret;
    }
}
\$\endgroup\$

closed as unclear what you're asking by Hosch250, forsvarir, t3chb0t, Ethan Bierlein, ferada Jan 9 '17 at 23:30

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Your "base must not be lower than 0" exception description is incorrect. Better, "base must not be lower than 1". I also prefer to include the name of the method throwing the exception: "BaseConversion.ltob: base must not be lower than 1", but that is optional. \$\endgroup\$ – rossum Jan 8 '17 at 22:36
  • \$\begingroup\$ Your unary is wrong. Unary 5 = 11111. Every position in unary is 1^n = 1, so there as many 1s as the size of the number. A minor change to your code. \$\endgroup\$ – rossum Jan 8 '17 at 22:41
1
\$\begingroup\$
        if(base==1){
            String ret="";
            while(v-->=0){
               ret+="0";
            }
            return ret;
        }

You should use a StringBuilder (or StringBuffer) rather than String concatenation.

        if (base == 1) {
            StringBuilder result = new StringBuilder((int)v);
            while (v-- >= 0) {
               result.append("0");
            }

            return result.toString();
        }

Note that this won't work if v is greater than Integer.MAX_VALUE. You also shouldn't expect to be able to create a String that long. I'll leave it up to you how you want to handle that situation.

There are a number of other options to do this. This is just the closest to your original code.

Note: I'm ignoring the question of the correct character to use in unary. It's obviously trivial to change it to 1 here.

This should also be true for other bases, although it's harder to determine the correct string length. Note that you can append each digit and then reverse the result at the end.

For bases up to hexatrigesimal (36), you can just use the built in toString. I.e.

        return Long.toString(v, base);

Obviously your code handles additional situations that that won't. For example, I don't think that handles unary.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.