JAVA——Unicode编码格式工具类

    科技2025-05-19  39

    Maven

    <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.3</version> </dependency>

    解决方案 

    HexadecimalUtil 

    package cn.edu.zstu.myzstu.utils.core.util; import cn.hutool.core.util.HexUtil; /** * @author ShenTuZhiGang * @version 1.0.0 * @date 2020-09-23 13:24 */ public class HexadecimalUtil extends HexUtil { /** * 用于建立十六进制字符的输出的小写字符数组 */ private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; /** * 用于建立十六进制字符的输出的大写字符数组 */ private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; public static String toHexadecimal(char ch) { return toHexadecimal(ch,false); } /** * 将指定char值转换为Unicode字符串形式,常用于特殊字符(例如汉字)转Unicode形式<br> * 转换的字符串如果不足4位,则前面用0填充,例如: * * <pre> * '我' =4f60 * </pre> * * @param ch char值 * @return Unicode表现形式 * @since 4.0.1 */ public static String toHexadecimal(char ch,Boolean toUpperCase) { final char [] DIGITS = toUpperCase?DIGITS_UPPER:DIGITS_LOWER; return "" +// DIGITS[(ch >> 12) & 15] +// DIGITS[(ch >> 8) & 15] +// DIGITS[(ch >> 4) & 15] +// DIGITS[(ch) & 15]; } }

    UnicodeUtil 

    package cn.edu.zstu.myzstu.utils; import cn.edu.zstu.myzstu.utils.core.util.HexadecimalUtil; import cn.hutool.core.text.StrBuilder; import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.StrUtil; /** * 提供Unicode字符串和普通字符串之间的转换 * * @author ShenTuZhiGang * @version 1.0.0 * @date 2020-09-22 23:59 */ public class UnicodeUtil { /** * Unicode字符串转为普通字符串<br> * Unicode字符串的表现方式为:\\uXXXX * * @param unicode Unicode字符串 * @return 普通字符串 */ public static String toString(String unicode) { if (StrUtil.isBlank(unicode)) { return unicode; } final int len = unicode.length(); StrBuilder sb = StrBuilder.create(len); int i; int pos = 0; while ((i = StrUtil.indexOfIgnoreCase(unicode, "\\u", pos)) != -1) { sb.append(unicode, pos, i);//写入Unicode符之前的部分 pos = i; if (i + 5 < len) { char c; try { c = (char) Integer.parseInt(unicode.substring(i + 2, i + 6), 16); sb.append(c); pos = i + 6;//跳过整个Unicode符 } catch (NumberFormatException e) { //非法Unicode符,跳过 sb.append(unicode, pos, i + 2);//写入"\\u" pos = i + 2; } } else { //非Unicode符,结束 break; } } if (pos < len) { sb.append(unicode, pos, len); } return sb.toString(); } /** * 字符串编码为Unicode形式 * * @param str 被编码的字符串 * @return Unicode字符串 */ public static String toUnicode(String str) { return toUnicode(str, true); } /** * 字符串编码为Unicode形式 * * @param str 被编码的字符串 * @param format 16进制Unicode编码格式 * @return */ public static String toUnicode(String str,UnicodeEncodingFormat format) { return toUnicode(str, true,format); } /** * 字符串编码为Unicode形式 * * @param str 被编码的字符串 * @param isSkipAscii 是否跳过ASCII字符(只跳过可见字符) * @return Unicode字符串 */ public static String toUnicode(String str, boolean isSkipAscii) { return toUnicode(str, isSkipAscii, UnicodeEncodingFormat.BACKSLASH_U); } /** * 字符串编码为Unicode形式 * * @param str 被编码的字符串 * @param isSkipAscii 是否跳过ASCII字符(只跳过可见字符) * @param format 16进制Unicode编码格式 * @return Unicode字符串 */ public static String toUnicode(String str, boolean isSkipAscii,UnicodeEncodingFormat format) { if (StrUtil.isEmpty(str)) { return str; } final int len = str.length(); final StrBuilder unicode = StrBuilder.create(str.length() * 6); char c; for (int i = 0; i < len; i++) { c = str.charAt(i); if (isSkipAscii && CharUtil.isAsciiPrintable(c)) { unicode.append(c); } else { unicode.append(String.format(format.format,HexadecimalUtil.toHexadecimal(c),format.toUpperCase)); } } return unicode.toString(); } /** * 16进制Unicode编码格式 */ public enum UnicodeEncodingFormat{ BACKSLASH_U("\\u%s",false), BACKSLASH("\\%s",true), BRACKETS("[%s]",false); public String format; public Boolean toUpperCase; UnicodeEncodingFormat(String format,Boolean toUpperCase){ this.format = format; this.toUpperCase = toUpperCase; } } }

    参考文章

    java中unicode utf-8以及汉字之间的转换工具类

    Processed: 0.009, SQL: 8