import java.util.HashMap;
public class test2 {
// 定义45进制数字
private static final String X45 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./: ";
// 拿到45进制转换10进制的值键对
private static HashMap<Character, Integer> _45To10 = createMap45To10();
// 拿到10进制转换45进制的值键对
private static HashMap<Integer, Character> _10To45 = createMap10To45();
// 定义静态进制数
private static int BASE = 45;
private static HashMap<Character, Integer> createMap45To10() {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < X45.length(); i++) {
map.put(X45.charAt(i), i);
}
return map;
}
private static HashMap<Integer, Character> createMap10To45() {
HashMap<Integer, Character> map = new HashMap<Integer, Character>();
for (int i = 0; i < X45.length(); i++) {
map.put(i, X45.charAt(i));
}
return map;
}
/**
* 10 to 45
*
* @param iSrc
* @return
*/
public static String _10To45(int iSrc) {
String result = "";
int key;
int value;
key = iSrc / BASE;
value = iSrc - key * BASE;
if (key != 0) {
result = result + _10To45(key);
}
result = result + _10To45.get(value).toString();
return result;
}
/**
* 45 to 10
*
* @param pStr 45进制字符串
* @return 十进制
*/
public static int _45To10(String pStr) {
if (pStr == "")
return 0;
// 目标十进制数初始化为0
int deciaml = 0;
// 记录次方,初始为45进制长度 -1
int power = pStr.length() - 1;
// 将45进制字符串转换成char[]
char[] keys = pStr.toCharArray();
for (int i = 0; i < pStr.length(); i++) {
// 拿到45进制对应的10进制数
int value = _45To10.get(keys[i]);
deciaml = (int) (deciaml + value * Math.pow(BASE, power));
// 执行完毕 次方自减
power--;
}
return deciaml;
}
public static void main(String[] args) {
int x = _45To10("AAA");
System.out.println(x);
//String binStr = Integer.toBinaryString(x);
//System.out.println(binStr);
//System.out.println(Integer.parseInt(binStr, 2));
String s = _10To45(11);
System.out.println(s);
}
}
输出:
20710
B