publicstatic String encode(int id){ int x = id / len; int y = id % len; return encode(x, y, ""); }
publicstatic String encode(int x, int y, String sb){ sb = array[y] + sb; if (x == 0) return sb; return encode(x / len, x % len, sb); }
publicstaticintdecode(String code){ int r = 0; for (int i = 0; i < code.length(); i++) { int fd = code.length() - i - 1; int x = enstr.indexOf(code.charAt(i)); if (fd != 0) { int cur = x * len; for (int j = 1; j < fd; j++) cur *= len; r += cur; } else r += x; } // for return r; }
privatestatic String baseSecret = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; privatestaticint len = baseSecret.length(); privatestatic Random ran = new Random();
publicstatic String encode(int id){ int x = id / len; int y = id % len; int r = ran.nextInt(baseSecret.length()); String secret = getOffsetSecret(r); String code = encode(x, y, "", secret) + baseSecret.charAt(r); return code + getCheckCode(code); }
publicstatic String encode(int x, int y, String sb, String secret){ sb = secret.charAt(y) + sb; if (x == 0) return sb; return encode(x / len, x % len, sb, secret); }// mtehod
code = code.substring(0, code.length() - 1); int r = 0; for (int i = 0; i < code.length(); i++) { int fd = code.length() - i - 1; int x = secret.indexOf(code.charAt(i)); if (fd != 0) { int cur = x * len; for (int j = 1; j < fd; j++) cur *= len; r += cur; } else r += x; } // for return r; }// method
//加入随机数 publicstatic String getOffsetSecret(int offset){ StringBuffer sb = new StringBuffer(); for (int i = 1; i <= baseSecret.length(); i++) { int o = (i + offset) % baseSecret.length(); sb.append(baseSecret.charAt(o)); } return sb.toString(); }// method
//加入校验码 publicstatic String getCheckCode(String str){ str = str.substring(0, str.length() - 1); int code = 0; for (int i = 0; i < str.length(); i++) code += baseSecret.indexOf(str.charAt(i)); return baseSecret.charAt(code % baseSecret.length()) + ""; }//