Class Base62Util
java.lang.Object
com.sure.tool.codec.Base62Util
Base62 编解码工具,字母表为
0-9A-Za-z。
Base62 常用于短链接、订单号、分享码等需要紧凑且 URL 安全的场景。
编码保留原始字节的前导零字节(输出为等数量的 0),解码时原样恢复。
示例:
String encoded = Base62Util.encode("hello".getBytes(StandardCharsets.UTF_8)); // "7tQLFHz"
String plain = Base62Util.decodeToString("7tQLFHz"); // "hello"
boolean valid = Base62Util.isBase62("7tQLFHz"); // true
null 语义:入参为 null 时返回 null(空安全);
解码遇到非 Base62 字母表字符抛 IllegalArgumentException。
- Since:
- 1.19.1
- Author:
- suretool
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]解码 Base62 字符串为字节数组。static StringdecodeToString(String base62) 解码 Base62 字符串为 UTF-8 字符串。static StringdecodeToString(String base62, Charset charset) 解码 Base62 字符串为字符串。static Stringencode(byte[] data) 编码字节数组为 Base62 字符串。static String编码字符串为 Base62 字符串(默认 UTF-8)。static booleanisBase62(CharSequence text) 判断字符串是否为合法的 Base62 文本。
-
Method Details
-
encode
编码字节数组为 Base62 字符串。示例:
Base62Util.encode("hello".getBytes(UTF_8)) -> "7tQLFHz"- Parameters:
data- 待编码字节,可为null- Returns:
- Base62 字符串;
null返回null
-
encode
-
decode
解码 Base62 字符串为字节数组。示例:
Base62Util.decode("7tQLFHz") -> "hello" 的 UTF-8 字节- Parameters:
base62- Base62 字符串,可为null- Returns:
- 解码字节数组;
null返回null,空串返回空数组 - Throws:
IllegalArgumentException- 含非 Base62 字符
-
decodeToString
-
decodeToString
-
isBase62
判断字符串是否为合法的 Base62 文本。- Parameters:
text- 待校验文本,可为null- Returns:
- 非空且所有字符均在 Base62 字母表内返回
true
-