項目中頻繁遇到二進制數據編碼、數據庫 Blob 流讀取、金額大寫轉換等場景。本文提供一個開箱即用的 ConvertUtil 工具類,涵蓋四大核心功能,代碼健壯、註解完善,可直接集成至項目公共模塊。
| 方法 | 功能描述 | 典型場景 |
|---|---|---|
bytesToHexString(byte[]) | 字節數組 → 十六進制字符串(大寫,不足兩位補 0) | 加密摘要輸出、二進制日志記錄、網絡協議調試 |
hexStringToBytes(String) | 十六進制字符串 → 字節數組 | 解析十六進制配置、還原加密密文、硬件指令構造 |
blobToBytes(Blob) | JDBC Blob 對象 → 字節數組(緩衝流讀取) | 數據庫圖片/文件導出、二進制字段讀取 |
transition(String) | 阿拉伯數字字符串 → 中文小寫(支持至十億) | 金額中文顯示、發票打印、合同生成 |
public class ConvertUtil {
/** 字節數組轉十六進制字符串 */
public static String bytesToHexString(byte[] src) {
if (src == null || src.length == 0) return null;
StringBuilder sb = new StringBuilder();
for (byte b : src) {
int v = b & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) sb.append('0');
sb.append(hv);
}
return sb.toString().toUpperCase();
}
/** 十六進制字符串轉字節數組 */
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.isEmpty()) return null;
hexString = hexString.toUpperCase();
int len = hexString.length() / 2;
char[] chars = hexString.toCharArray();
byte[] d = new byte[len];
for (int i = 0; i < len; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(chars[pos]) << 4 | charToByte(chars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/** Blob 轉字節數組 */
public static byte[] blobToBytes(Blob blob) {
if (blob == null) return null;
try (BufferedInputStream is = new BufferedInputStream(blob.getBinaryStream())) {
byte[] bytes = new byte[(int) blob.length()];
int offset = 0, read;
while (offset < bytes.length && (read = is.read(bytes, offset, bytes.length - offset)) != -1) {
offset += read;
}
return bytes;
} catch (Exception e) {
return null; // 生產環境建議拋出自定義異常或記錄日志
}
}
/** 阿拉伯數字轉中文小寫(支持 10 位以內整數) */
public static String transition(String si) {
String[] unit = {"", "十", "百", "千", "萬", "十萬", "百萬", "千萬", "億", "十億"};
String[] digit = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
char[] ch = si.toCharArray();
int n = ch.length;
StringBuilder res = new StringBuilder();
if (n == 2) { // 兩位數特殊處理:十、十一...十九
for (int i = n - 1, j = 0; i >= 0; i--, j++) {
if (ch[j] != '0') {
if (j == 0 && ch[j] == '1') res.append(unit[i]);
else res.append(digit[ch[j] - '1']).append(unit[i]);
}
}
} else { // 通用邏輯
for (int i = n - 1, j = 0; i >= 0; i--, j++) {
if (ch[j] != '0') res.append(digit[ch[j] - '1']).append(unit[i]);
}
}
return res.toString();
}
}
public static void main(String[] args) {
// 1. Hex 雙向轉換
byte[] data = "Hello".getBytes(StandardCharsets.UTF_8);
String hex = ConvertUtil.bytesToHexString(data); // 48656C6C6F
byte[] restored = ConvertUtil.hexStringToBytes(hex);
System.out.println(new String(restored, StandardCharsets.UTF_8)); // Hello
// 2. 數字轉中文
System.out.println(ConvertUtil.transition("0")); // (空串,建議調用方處理 0)
System.out.println(ConvertUtil.transition("5")); // 五
System.out.println(ConvertUtil.transition("10")); // 十
System.out.println(ConvertUtil.transition("123")); // 一百二十三
System.out.println(ConvertUtil.transition("10000")); // 一萬
System.out.println(ConvertUtil.transition("100000000")); // 一億
}
blob.length() 可能超過 Integer.MAX_VALUE,大文件建議分塊流式處理而非一次性載入內存。NumberToCN)或自行擴展。IllegalArgumentException 或自定義異常,避免吞噬異常導致排查困難。