标签:scale 符号位 sharp algorithm 去掉 sys 补码 import eve
流水号设计,要求数据库里存int,界面显示的时候,显示为33进制(26个字母+10个数字,去掉容易混淆的字母I, L, O)
其实要求就是int与33进制互相转换.
具体实现如下:
package com.liu.algorithm;
import java.util.Arrays;
public class Notation {
public static void main(String[] args) {
String param = transform(1234567890,33);
System.out.println("转换的进制的数为:"+ param);
System.out.println("原始数为:"+recover(param,33));
}
// 系数集合
public static char[] coefficients = {‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,
‘H‘,‘J‘,‘K‘,‘M‘,‘N‘,‘P‘,‘Q‘,‘R‘,‘S‘,‘T‘,‘U‘,‘V‘,‘W‘,‘X‘,‘Y‘,‘Z‘};
/**
* 进制转换
* @param origin 需要转换的原始数值
* @param notations 基数(进制)
* @return 转换后的进制数
*/
private static String transform(int origin,int notations) {
StringBuilder sb = new StringBuilder();
String result;
while(origin != 0){
sb = sb.append(coefficients[origin%notations]);
origin = origin/notations;
}
result = sb.reverse().toString();
return result;
}
/**
* 获取原始数值
* @param origin 转换后的进制数
* @param notations 基数(进制)
* @return 原始数值
*/
private static int recover(String origin,int notations) {
StringBuilder sb = new StringBuilder(origin);
String string = sb.reverse().toString();
char[] chars = string.toCharArray();
int result = 0;
int scale = 1;
for (char cha : chars) {
int coefficient = Arrays.binarySearch(coefficients,cha);
result += coefficient*scale;
scale *= notations;
}
return result;
}
}
标签:scale 符号位 sharp algorithm 去掉 sys 补码 import eve
原文地址:https://www.cnblogs.com/chengmuyu/p/10703284.html