/**进制转换工具,支持2-36任意进制间相互转换
*/
public class HexConverter {
public static String Quotient = null;
public static void main(String[] args) {
String a = baseConv("45435424", 10, 34);
System.out.println("10转34输出值:" + a);
System.out.println("34转10输出值:" + baseConv(a, 34, 10));
}
public static String baseConv(String in, int fr, int to) {
if (fr < 2 || fr > 36 || to < 2 || to > 36) {
return null;
}
String out = "";
Quotient = in;
while (Quotient.length() > 0) {
out = getRemainder(Quotient, fr, to) + out;
}
return out;
}
public static String getRemainder(String s, int fr, int to) {
Quotient = "";
int temp = 0;
while (s.length() > 0) {
int t = str2Int(s.substring(0, 1));
s = s.substring(1);
temp = temp * fr + t;
Quotient += int2Str(temp / to);
temp = temp % to;
}
while (Quotient.length() > 0 && Quotient.charAt(0) == '0') {
Quotient = Quotient.substring(1);
}
return int2Str(temp);
}
public static int str2Int(String s) {
return s.charAt(0) <= '9' && s.charAt(0) >= '0' ? s.charAt(0) - '0' : s
.charAt(0) - 'a' + 10;
}
public static String int2Str(int i) {
return i >= 0 && i <= 9 ? String.valueOf((char) ('0' + i)) : String
.valueOf((char) ('a' + i - 10));
}