import java.math.BigDecimal; import java.util.LinkedList; import java.util.List; import java.util.stream.IntStream; /** * 26/52进制与10进制互转工具类 * * @author * * */ public class Decimal52 { private static final int LOWER_A = 97; private static final int LOWER_Z = 123; private static final int UPPER_A = 65; private static final int UPPER_Z = 91; private static final int ZERO = 48; private static final int NINE = 58; private List<Character> C_LIST = new LinkedList<Character>(); private int charSize; public Decimal52() { this(true, false, true); } public Decimal52(boolean upperOnly, boolean lowerOnly, boolean hasNum) { if (hasNum) { IntStream.range(ZERO, NINE).forEach((c) -> C_LIST.add((char) c)); } if (upperOnly) { IntStream.range(UPPER_A, UPPER_Z).forEach( (c) -> C_LIST.add((char) c)); } else if (lowerOnly) { IntStream.range(LOWER_A, LOWER_Z).forEach( (c) -> C_LIST.add((char) c)); } else { IntStream.range(UPPER_A, UPPER_Z).forEach( (c) -> C_LIST.add((char) c)); IntStream.range(LOWER_A, LOWER_Z).forEach( (c) -> C_LIST.add((char) c)); } charSize = C_LIST.size(); } /** * @version 1.0.0 * @Description 10进制转26/52进制 * @return * */ public String getDecimal(int num) { if (num < 0) return null; StringBuilder sb = new StringBuilder(); if (num >= charSize) { sb.append(this.getDecimal(num / charSize)); } int least = num % charSize; sb.append(C_LIST.get(least)); return sb.toString(); } /** * @version 1.1 * @Description N进制转10进制<br/> * null非法字符转换 * @return * */ public BigDecimal getDecimalism(String str) { BigDecimal num = BigDecimal.valueOf(0); char[] cNum = str.toCharArray(); int length = cNum.length; for (char c : cNum) { if (C_LIST.indexOf(c) < 0) return null; num = num.add(BigDecimal.valueOf(C_LIST.indexOf(c)).multiply( BigDecimal.valueOf(Math.pow(charSize, (--length))))); } return num; } /** * @version 1.0.0 * @Description 10进制转N进制 * @return * */ public String getDecimal(BigDecimal num) { if (num.compareTo(BigDecimal.valueOf(0)) < 0) { return null; } StringBuilder sb = new StringBuilder(); BigDecimal[] divideAndRemainder = num.divideAndRemainder(BigDecimal .valueOf(charSize)); if (divideAndRemainder[0].intValue() > 0) { sb.append(this.getDecimal(divideAndRemainder[0])); } int least = divideAndRemainder[1].intValue(); sb.append(C_LIST.get(least)); return sb.toString(); } }
10进制与64进制互相转换
import org.apache.commons.lang.StringUtils;
public class ConversionUtil {
private static String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static int scale = 62;
private static int minLength = 5;
//数字转62进制
public static String encode(long num) {
StringBuilder sb = new StringBuilder();
int remainder;
while (num > scale - 1) {
//对 scale 进行求余,然后将余数追加至 sb 中,由于是从末位开始追加的,因此最后需要反转字符串
remainder = Long.valueOf(num % scale).intValue();
sb.append(chars.charAt(remainder));
//除以进制数,获取下一个末尾数
num = num / scale;
}
sb.append(chars.charAt(Long.valueOf(num).intValue()));
String value = sb.reverse().toString();
return StringUtils.leftPad(value, minLength, '0');
}
//62进制转为数字
public static long decode(String str) {
//将 0 开头的字符串进行替换
str = str.replace("^0*", "");
long value = 0;
char tempChar;
int tempCharValue;
for (int i = 0; i < str.length(); i++) {
//获取字符
tempChar = str.charAt(i);
//单字符值
tempCharValue = chars.indexOf(tempChar);
//单字符值在进制规则下表示的值
value += (long) (tempCharValue * Math.pow(scale, str.length() - i - 1));
}
return value;
}
}
转载https://blog.csdn.net/MDJHZGJ/article/details/78440265?utm_source=blogxgwz3