0-9999999999999之间long类型的数值转换为长度为8的字符串(62进制)

import org.apache.commons.lang3.StringUtils;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* 0 - 9999999999999
*/
public class TermInfoUtils {
  static Logger logger
    = Logger.getLogger(
    TermInfoUtils.class.getName());

  public static void main(String[] args) {
    generateUnionDeviceId(9999999999999l);
  }

  /**
  * https://en.wikipedia.org/wiki/Base62#/
  * wiki上的标准
  */
  private final static char[] charArray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();

  /**
  * base10 转 base 62
  *
  * @param base10
  * @return
  */
  public static String fromBase10ToBase62(long base10) {
    int scale = 62;
    StringBuilder sb = new StringBuilder();
    long remainder = 0;
    do {
      remainder = base10 % scale;
      sb.append(charArray[(int) remainder]);
      base10 = base10 / scale;
    } while (base10 > scale - 1);
    sb.append(charArray[(int) base10]);
    // 倒序
    return sb.reverse().toString();
  }

  /**
  * 产生银联终端号
  *
  * @param base10
  * @return
  */
  public static String generateUnionDeviceId(long base10) {
    String base62 = fromBase10ToBase62(base10);
    // 往左补 0
    logger.log(Level.INFO, base62 + "==============" + base10);
    return StringUtils.leftPad(base62, 8, "0");
  }
}

上一篇:adb通过wifi连接android设备


下一篇:gcc常用编译选项