Skip to content

Instantly share code, notes, and snippets.

@sbsatter
Created August 19, 2016 11:57
Show Gist options
  • Save sbsatter/89b064f22e9d32c19e818c40a699a376 to your computer and use it in GitHub Desktop.
Save sbsatter/89b064f22e9d32c19e818c40a699a376 to your computer and use it in GitHub Desktop.
Convert an integer to any base
class IntegerBaseConversion{
static String numbers= "0123456789abcdef";
public static void main(String ... args){
if(args.length!=2){
int n= Integer.parseInt(args[0]);
int base= Integer.parseInt(args[1]);
System.out.println(convertBase(n, base));
}
}
static String convertBase(int n, int base){
if(n<base){
return numbers.charAt(n)+"";
}
return convertBase(n/base, base)+numbers.charAt(n%base);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment