public static String decode(String str62) {
String[] ss = split(str62, 4);
StringBuilder sb = null;
for (String s : ss) {
s = toDecString(s);
if (sb == null) {
sb = new StringBuilder(s);
} else {
sb.append(format(s, 7));
}
}
return sb.toString();
}
private static String format(String str, int len) {
if (len <= str.length()) {
return str;
}
char[] cs = new char[len];
Arrays.fill(cs, '0');
return String.copyValueOf(cs, 0, len - str.length()) + str;
}
private static String toDecString(String str62) {
long res = 0;
int len = str62.length();
for (int ii = 0; ii < len; ii++) {
long atom = (long) Math.pow(62, len - ii - 1);
res = res + atom * convert2Int(str62.charAt(ii));
}
return String.valueOf(res);
}
private static int convert2Int(char c) {
if (c >= 48 && c <= 57) {//0-9
return c - 48;
} else if (c >= 97 && c <= 122) {//a-z
return c - 87;
} else if (c >= 65 && c <= 90) {//A-Z
return c - 29;
} else {//不支持的字符
return -1;
}
}
private static String[] split(String s, int len) {
int count = s.length();
int nn = count % len;
int size = (nn > 0) ? count / len + 1 : count / len;
String[] res = new String[size];
nn = nn > 0 ? nn : len;
res[0] = s.substring(0, nn);
for (int ii = 1; ii < size; ii++) {
res[ii] = s.substring((ii - 1) * len + nn, ii * len + nn);
}
return res;
}
public static void main(String[] args) {
Test test = new Test();
//http://weibo.com/3173644855/zDl0ZAVZC
String s=test.decode("zDl0ZAVZC");
System.out.println(s);
}
java base64
最新推荐文章于 2024-09-03 20:17:53 发布