最后更新日:2014年11月07日
class="java" name="code">
import java.util.HashMap;
import java.util.Map;
/**
* @author QuarterLifeForJava
* V0.2初级版本(你绝对可以写的更好)
* [只支持正整数,只简单进行了非法输入的判断]
* N进制到N进制转换:N进制->10进制->N进制
*/
public class Test {
//自定义字符集
public static final char[] CHARACTERSET = new char[]{
'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','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','`','~','!','@','#','$','%','^',
'&','*','(',')','-','_','=','+','{','}',
'[',']',';',':','"','\'','\\','|','?','.',
'/','<','>','您','好','世','界'
};
//自定义字符键(字符)值(十进制值)对
//`~!@#$%^&*()-_=+{}[];:\"'\\|?.'/<>您好世界
public static final Map<String,String> CHARACTEKV = new HashMap<String,String>();
static{
CHARACTEKV.put("0", "0");CHARACTEKV.put("1", "1");CHARACTEKV.put("2", "2");
CHARACTEKV.put("3", "3");CHARACTEKV.put("4", "4");CHARACTEKV.put("5", "5");
CHARACTEKV.put("6", "6");CHARACTEKV.put("7", "7");CHARACTEKV.put("8", "8");
CHARACTEKV.put("9", "9");CHARACTEKV.put("a", "10");CHARACTEKV.put("b", "11");
CHARACTEKV.put("c", "12");CHARACTEKV.put("d", "13");CHARACTEKV.put("e", "14");
CHARACTEKV.put("f", "15");CHARACTEKV.put("g", "16");CHARACTEKV.put("h", "17");
CHARACTEKV.put("i", "18");CHARACTEKV.put("j", "19");CHARACTEKV.put("k", "20");
CHARACTEKV.put("l", "21");CHARACTEKV.put("m", "22");CHARACTEKV.put("n", "23");
CHARACTEKV.put("o", "24");CHARACTEKV.put("p", "25");CHARACTEKV.put("q", "26");
CHARACTEKV.put("r", "27");CHARACTEKV.put("s", "28");CHARACTEKV.put("t", "29");
CHARACTEKV.put("u", "30");CHARACTEKV.put("v", "31");CHARACTEKV.put("w", "32");
CHARACTEKV.put("x", "33");CHARACTEKV.put("y", "34");CHARACTEKV.put("z", "35");
}
/**
* 十进制转化成任意进制数(辗转相除取余法)
* @param decimal 十进制数
* @param arbitraryHexadecimal 想转换成的进制数
* @param characterSet 自定义字符集
* @return String
*/
public static String decimal2AnyDecimal(long decimal,int arbitraryHexadecimal,char characterSet[]){
int characterSetLength = characterSet.length;
if(arbitraryHexadecimal>characterSetLength){
return "您的字符集的个数:"+characterSetLength+",不能小于"+"您所要转换成的进制数:"+arbitraryHexadecimal;
}
StringBuilder sb = new StringBuilder();
//余数初始化
int remainder = 0;
while(decimal > 0){
remainder = (int)decimal%arbitraryHexadecimal;
sb.append(characterSet[remainder]);
decimal/=arbitraryHexadecimal;
}
return sb.reverse().toString();
}
/**
* 任意进制数转化成十进制
* @param AnyDecimal 输入一个任意进制数
* @param arbitraryHexadecimal 是几进制数
* @param characteKV 自定义字符键(字符)值(十进制值)对
* @return long
*/
public static String AnyDecimal2decimal(String anyDecimal,int arbitraryHexadecimal,Map<String,String> characteKV){
int anyDecimalOfLength = anyDecimal.length();
long total = 0;
long number = 0;
for (int j = anyDecimalOfLength-1; j > -1; j--){
String value = characteKV.get(String.valueOf(anyDecimal.charAt(j)));
if(value==null){
return "进制数:"+anyDecimal+"中的"+anyDecimal.charAt(j)+"未自定义在字符键(字符)值(十进制值)对中";
}
number = (Integer.parseInt(value))*square(arbitraryHexadecimal, anyDecimalOfLength-j-1);
total = total + number;
}
return String.valueOf(total);
}
/**
* 递归求次幂
* @param baseNumber>=0 底数
* @param indexNumber>=0 指数
* @return int
*/
public static int square(int baseNumber,int indexNumber){
if(baseNumber==0){
return 0;
}else if(indexNumber==0){
return 1;
}else{
return baseNumber*square(baseNumber,--indexNumber);
}
}
}