Simple+Numbers+Conversion

Every integer number //n// is represented in positional number system of base //r// by a sequence of digits 0 <= //di// < //r//, so the value is equal to: //n// = //d//0 + //r// * //d//1 + //r2// * //d//2 + //r3// * //d//3 + ... Your task is to convert a given number in //r//-base represantation into //s//-base representation, for example: decimal 231 into binary 11100111. Assume that //r// <= 36 and the digits are 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//.

Input
//N// [the number of series <= 1000]

//n r s// [//n// <= 101000, //r//,//s// <= 36]

Output
//n// [//s//-base representation of number //n//] Text grouped in [ ] does not appear in the input and output file.

**Sample Input**
3

231 10 2

ABC 15 10

XYZ 36 2

**Sample Output**
11100111

2427

1010101111111011

Source Code code format="java5" public class SimpleNumbersConversion { public static void main(String[] args) { String n = "231"; int r = 10; int s = 2;

System.out.println(rBaseToSBase(n, r, s));

n = "ABC"; r = 15; s = 10;

System.out.println(rBaseToSBase(n, r, s));

n = "XYZ"; r = 36; s = 2;

System.out.println(rBaseToSBase(n, r, s)); }

// r-Base ǥ���Ŀ��� s-Base ǥ�������� ���� public static String rBaseToSBase(String n, int r, int s) { return decimalToSBase(rBaseToDecimal(n, r), s); }

// r-Base ǥ���Ŀ��� 10-Base ǥ�������� ���� public static int rBaseToDecimal(String n, int r) { int ret = 0;

for(int i = 0; i < n.length; i++) { ret *= r;           ret += convertToNumber(n.charAt(i)); }

return ret; }

// 10-Base ǥ���Ŀ��� s-Base ǥ�������� ���� public static String decimalToSBase(int dec, int s) { StringBuffer sb = new StringBuffer;

while(dec > 0) { sb.insert(0, convertToChar(dec % s)); dec /= s;       }

return sb.toString; }

// r-Base ǥ������ ���ڸ� 10-Base ǥ������ ���ڷ� ���� public static int convertToNumber(char ch) { if(ch >= '0' && ch <= '9') { return ch - '0'; } else if(ch >= 'A' && ch <= 'Z') { return ch - 'A' + 10; }

return -1; }

// 10-Base ǥ������ ���ڸ� r-Base ǥ������ ���ڷ� ���� public static char convertToChar(int i) { if(i >= 0 && i <= 9) { return (char)(i + '0'); } else if(i >= 10 && i <= 35) { return (char)(i + 'A' - 10); }       return 0x0000; } } code

Comment