由于项目需要把一个ID(数字)压缩成尽可能短字符串,在网上找来好久没有找到合适,
自己动手写一个。
有需要的可以参考一下,也作为自己的一些积累吧!
代码如下:
Java代码
- /**
- * 64进制和10进制的转换类
- * @author Administrator
- *
- */
- public class compressEncodeing {
- final static char[] digits = {
- ‘0‘ , ‘1‘ , ‘2‘ , ‘3‘ , ‘4‘ , ‘5‘ ,
- ‘6‘ , ‘7‘ , ‘8‘ , ‘9‘ , ‘a‘ , ‘b‘ ,
- ‘c‘ , ‘d‘ , ‘e‘ , ‘f‘ , ‘g‘ , ‘h‘ ,
- ‘i‘ , ‘j‘ , ‘k‘ , ‘l‘ , ‘m‘ , ‘n‘ ,
- ‘o‘ , ‘p‘ , ‘q‘ , ‘r‘ , ‘s‘ , ‘t‘ ,
- ‘u‘ , ‘v‘ , ‘w‘ , ‘x‘ , ‘y‘ , ‘z‘ ,
- ‘A‘ , ‘B‘ , ‘C‘ , ‘D‘ , ‘E‘ , ‘F‘ ,
- ‘G‘ , ‘H‘ , ‘I‘ , ‘J‘ , ‘K‘ , ‘L‘ ,
- ‘M‘ , ‘N‘ , ‘O‘ , ‘P‘ , ‘Q‘ , ‘R‘ ,
- ‘S‘ , ‘T‘ , ‘U‘ , ‘V‘ , ‘W‘ , ‘X‘ ,
- ‘Y‘ , ‘Z‘ , ‘+‘ , ‘/‘ ,
- };
- /**
- * @param args
- */
- public static void main(String[] args) {
- System.out.println(CompressNumber(999999999999999999L,6));
- System.out.println(UnCompressNumber(CompressNumber(999999999999999999L,6)));
- }
- /**
- * 把10进制的数字转换成64进制
- * @param number
- * @param shift
- * @return
- */
- private static String CompressNumber(long number, int shift) {
- char[] buf = new char[64];
- int charPos = 64;
- int radix = 1 << shift;
- long mask = radix - 1;
- do {
- buf[--charPos] = digits[(int)(number & mask)];
- number >>>= shift;
- } while (number != 0);
- return new String(buf, charPos, (64 - charPos));
- }
- /**
- * 把64进制的字符串转换成10进制
- * @param decompStr
- * @return
- */
- private static long UnCompressNumber(String decompStr)
- {
- long result=0;
- for (int i = decompStr.length()-1; i >=0; i--) {
- if(i==decompStr.length()-1)
- {
- result+=getCharIndexNum(decompStr.charAt(i));
- continue;
- }
- for (int j = 0; j < digits.length; j++) {
- if(decompStr.charAt(i)==digits[j])
- {
- result+=((long)j)<<6*(decompStr.length()-1-i);
- }
- }
- }
- return result;
- }
- /**
- *
- * @param ch
- * @return
- */
- private static long getCharIndexNum(char ch)
- {
- int num=((int)ch);
- if(num>=48&&num<=57)
- {
- return num-48;
- }
- else if(num>=97&&num<=122)
- {
- return num-87;
- }else if(num>=65&&num<=90)
- {
- return num-29;
- }else if(num==43)
- {
- return 62;
- }
- else if (num == 47)
- {
- return 63;
- }
- return 0;
- }
- }
时间: 2024-10-05 05:32:32