public class StringToNum{ static long stringToNum(String rep, int base){ long retVal=0; long placeVal=1; for (int i=rep.length()-1; i>=0; i--){ retVal=retVal+placeVal* (rep.charAt(i) - '0'); placeVal=placeVal*base; } return retVal; } public static void printConvert(String rep, int base){ System.out.println(rep + " in base " + base + " = " + stringToNum(rep,base)); } public static void main(String args[]){ printConvert("11011011",2); printConvert("7103",8); printConvert("123",10); printConvert("123704",8); } } /* cd /home/bremner/teach/1083/lecture/L12/ java -Djava.compiler=NONE StringToNum 101 in base 2 = 5 101 in base 7 = 50 123 in base 10 = 123 123 in base 8 = 83 Process StringToNum finished */