欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

生僻字与16进制的转换

程序员文章站 2022-04-12 20:50:03
...

字符串中的生僻字在传输和存储的过程中存在着各种各样的问题,为了便于传输和存储,我们通常会把这些生僻字转换为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文件见附件。

  • gbk.rar (90.8 KB)
  • 下载次数: 1