I want my code whenever I input a base from 2-36 , It can output a number which is decimal. here is my code
package code;
public class solution{
public static int x2ten(String s, int base){
String[] bits = s.split("");
int result = 0;
for(int i=0; i < bits.length; i++ ){
int val = (Integer.parseInt(bits[bits.length-1-i])) * ((int)Math.pow(base,i));
val = charToInt(Integer.parseInt(bits[bits.length-1-i]))* ((int)Math.pow(base,i));
result += val;
}
return result;
}
public static int charToInt(char c){
char[] characters = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G',
'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for(int j = 0; j<characters.length; j++){
if(characters[j] == c){
return j;
}
}
return -1;
}
public static void main(String[] args)
{
System.out.println(x2ten("1101",2));
}
}
There is a problem at first appear charToInt, it said that I should change charToInt(char c) to charToInt(int c). But whats purpose of charToInt is convert whatever I input a Character like 1A, then it can output 26 in decimal.