例一:
/*
任意进制间的转换
n 进制转 m 进制
String s = "2001201102";
3进制转为5进制
*/
public class T1
{
public static void main(String[] args)
{
String s = "2001201102";
int n = 0;
for(int i=0; i<s.length(); i++){
n = n*3 + (s.charAt(i)-'0');
}
System.out.println(n);
String s2 = "";
for(;;){
if(n==0) break;
s2 = (n%5) + s2;
n = n / 5;
}
System.out.println(s2);
}
}
例二:
/*第02讲-进制的转换_编程
编程
Excel 单元格地址有两种格式:
普通格式,如:A5, BC12
对应的RC格式:R5C1, R12C55
显然,RC格式是直接给出行号和列号
请编程在两种地址格式间转换。*/
//普通格式的列是用字母表示的26进制数,BC = ('B' - 'A' + 1) * 26 + ('C' - 'A' + 1);
public class C3 {
public static void main(String[] args) {
String s = "BCA1234";
char []c = s.toCharArray();
int row = 0 ,clum = 0; //行号和列号
int i = 0;
while (c[i] >= 'A' && c[i] <= 'Z' && i < c.length){//转换列
clum = clum * 26 + (c[i] - 'A' + 1);
++i;
}
//转换行
while (i < c.length){
row = row * 10 + (c[i] - '0');
i++;
}
String s2 = "R" + row + "C" + clum;
System.out.println(s2);
}
}