• java Base36 算法


    package com.github.linushp.wsblog.utils;
    
    
    import java.math.BigInteger;
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    
    public class Base36 {
    
        private static final Charset CHAT_SET_UTF8 = StandardCharsets.UTF_8;
    
        public static String encode(byte[] inputBytes) {
            if (inputBytes == null || inputBytes.length == 0) {
                return "";
            }
            BigInteger bigInteger = new BigInteger(inputBytes);
            String xx = bigInteger.toString(36);
            if (xx.charAt(0) == '-') {
                StringBuffer stringBuffer = new StringBuffer(xx);
                stringBuffer.setCharAt(0, 'f');
                return stringBuffer.toString();
            } else {
                return "z" + xx;
            }
        }
    
        public static String encode(String str) {
            if (str == null || str.isEmpty()) {
                return "";
            }
            return encode(str.getBytes(CHAT_SET_UTF8));
        }
    
        public static byte[] decode(String str) {
            if (str == null || str.isEmpty()) {
                return new byte[]{};
            }
    
            String str1;
            if (str.charAt(0) == 'z') {
                str1 = str.substring(1);
            } else {
                str1 = "-" + str.substring(1);
            }
            BigInteger bigInteger = new BigInteger(str1, 36);
            return bigInteger.toByteArray();
        }
    
        public static String decodeStr(String str) {
            if (str == null || str.isEmpty()){
                return "";
            }
            byte[] bytes1 = decode(str);
            return new String(bytes1, CHAT_SET_UTF8);
        }
    
    }
    

      

  • 相关阅读:
    echarts常用说明
    ionic4打包和ngix配置
    浏览器onbeforeunload
    vue中keepAlive的使用
    面试题
    git使用
    git常用命令
    ajax原理
    关于npm(一)
    React、Vue、Angular对比 ---- 新建及打包
  • 原文地址:https://www.cnblogs.com/lhp2012/p/10214760.html
Copyright © 2020-2023  润新知