十進数(10)と64進数の相互変換アルゴリズム
7221 ワード
十進数(10)と64進数の相互変換アルゴリズム
適用先 URLを短縮して、二次元コードの鮮明度を増加します。 字数制限のミニブログ、文章共有などがあります。 数字の復号化 JAVA実現コード
適用先
import java.security.MessageDigest;
import java.util.Stack;
/**
* Created By 2017 6 17 22:17:04(TEL:15197447018)
*
* @version 2.0
*
*
*
*/
public class PECode {
public static void main(String[] args) {
System.out.println("64 :" + encode(201314520));
System.out.println("10 :" + decode(encode(201314520)));
}
/**
* ,
*/
private static final char[] array = { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g','h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '8', '5', '2', '7', '3', '6', '4', '0', '9', '1', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '+', '-' };
/**
* @param number
* double 10 , 0
* @return string 64
*/
public static String encode(double number) {
double rest = number;
//
Stack stack = new Stack();
StringBuilder result = new StringBuilder(0);
while (rest >= 1) {
// ,
// (rest - (rest / 64) * 64)
stack.add(array[new Double(rest % 64).intValue()]);
rest = rest / 64;
}
for (; !stack.isEmpty();) {
//
result.append(stack.pop());
}
return result.toString();
}
/**
* A-Z,a-z,0-9,+,-
*
* @param str 64
* @return 10
*/
public static double decode(String str) {
//
int multiple = 1;
double result = 0;
Character c;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(str.length() - i - 1);
result += decodeChar(c) * multiple;
multiple = multiple * 64;
}
return result;
}
private static int decodeChar(Character c) {
for (int i = 0; i < array.length; i++) {
if (c == array[i]) {
return i;
}
}
return -1;
}
}