I’m working on a game where the player going to encounter a alien race, which is suppose to have a digit up to 16. I have manage to make the code that can translate Alien to Human. But not Human to Alien. I’m using A-G in the code for the 10-16 digits. How do I make the Human to Alien method?
Just in case someone wants/need to see my code that translate Alien to Human.
public static int fromAlienNumber(String nr){
boolean isNegative = nr.contains("-");
//Code to see if the number is compatible with the alien alphabet.
Matcher m = Pattern.compile("(-?[0-9A-G]+)").matcher(nr);
if(!m.matches()){
System.err.println(nr+" is not an alien number");
return -1;
}
int result = 0;
nr = nr.replace("-","");
for(int i=0; i<nr.length(); i++)
result += (int)(charToNumber(nr.charAt(i)) * Math.pow(10, nr.length()-(i+1)));
return isNegative ? -result : result;
}
public static int charToNumber(char c){
switch(c){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
case 'G': return 16;
}
//To make the compiler happy, it should never come here.
return -1;
}