Created
August 19, 2016 11:57
-
-
Save sbsatter/89b064f22e9d32c19e818c40a699a376 to your computer and use it in GitHub Desktop.
Convert an integer to any base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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