生僻字与16进制的转换 博客分类: Java编程 生僻字16进制转换java
程序员文章站
2024-03-16 15:07:58
...
字符串中的生僻字在传输和存储的过程中存在着各种各样的问题,为了便于传输和存储,我们通常会把这些生僻字转换为16进制编码进行传输和存储。
package com.dc.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; /* ****************** 类说明 ********************* * class : RareWordUtil * @author : ncc * create time : 2017-12-20 上午09:49:54 * @version : 1.0 * description : 生僻字与16进制编码相互转化 * @see : * ************************************************/ public class RareWordUtil { private static Map<String, String> nameToValue; private static Map<String, String> valueToName; private String fileName = "gbk.txt"; private String fileEncoding = "GBK"; private final String preFix = "■△"; private static final RareWordUtil cr_instance = new RareWordUtil(); /** * 构造方法 */ private RareWordUtil() { try { init(); } catch (IOException e) { System.out.println(e.getMessage()); } } /* ******************************************** * method name : getInstance * description : 获取实例 * @return : RareWordUtil * @param : @return * modified : ncc , 2017-12-20 * @see : * ********************************************/ public static RareWordUtil getInstance() { return cr_instance; } /* ******************************************** * method name : init * description : 初始化 * @return : void * @param : @throws IOException * modified : ncc , 2017-12-20 * @see : * ********************************************/ @SuppressWarnings( { "static-access", "unchecked" }) private void init() throws IOException { if (nameToValue != null && valueToName != null) { return; } InputStream input = RareWordUtil.class.getResourceAsStream(fileName); InputStreamReader inputsr = new InputStreamReader(input, Charset.forName(fileEncoding)); BufferedReader br = new BufferedReader(inputsr); this.nameToValue = new HashMap(); this.valueToName = new HashMap(); String line = null; while ((line = br.readLine()) != null) { String ret = new String(line.getBytes(fileEncoding), fileEncoding); String key = ret.substring(0, 1); String value = ret.substring(line.length() - 4, line.length()); this.nameToValue.put(key, value); this.valueToName.put(value, key); } } /* ******************************************** * method name : getRareWord * description : 将含16进制编码的字符串转化成含生僻字的字符串 * @return : String * @param : @param value * @param : @return * modified : ncc , 2017-12-20 * @see : * ********************************************/ @SuppressWarnings("static-access") public String getRareWord(String value) { StringBuffer sb = new StringBuffer(); int pos = -1; while ((pos = value.indexOf(preFix)) != -1) { sb.append(value.substring(0, pos)); String code = value.substring(pos + preFix.length(), pos + preFix.length() + 4); String codeValue = this.valueToName.get(code); sb.append(codeValue); value = value.substring(pos + preFix.length() + 4, value.length()); } sb.append(value); return sb.toString(); } /* ******************************************** * method name : getHex * description : 将含生僻字的字符串转化成含16进制编码的字符串 * @return : String * @param : @param name * @param : @return * modified : ncc , 2017-12-20 * @see : * ********************************************/ @SuppressWarnings("static-access") public String getHex(String name) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < name.length(); i++) { String value = this.nameToValue.get(name.substring(i, i + 1)); if (value == null) { sb.append(name.substring(i, i + 1)); } else { sb.append(preFix + value); } } return sb.toString(); } public static void main(String[] args) { RareWordUtil cu = RareWordUtil.getInstance(); String name = "王喆昊"; name = cu.getHex(name); System.out.println("Cent2Dollar(1) = " + name); name = cu.getRareWord(name); System.out.println("Cent2Dollar(1) = " + name); } }
代码中用的的txt文件见附件。
推荐阅读