Conversion of Rare Words and Hexadecimal

There are various problems in the process of transmission and storage of rare words in strings. In order to facilitate transmission and storage, we usually convert these rare words into hexadecimal codes for transmission and storage.

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 description ********************
 * class       :  RareWordUtil
 * @author     :  ncc
 * create time : 2017-12-20 09:49:54 am
 * @version    :  1.0  
 * description : Uncommon characters and hexadecimal codes are converted into each other
 * @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();

	/**
	 * Construction method
	 */
	private RareWordUtil() {
		try {
			init();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	/* ********************************************
	 * method name   : getInstance
	 * description : get the instance
	 * @return       : RareWordUtil
	 * @param        : @return
	 * modified      : ncc ,  2017-12-20
	 * @see          :
	 * ********************************************/      
	public static RareWordUtil getInstance() {
		return cr_instance;
	}

	/* ********************************************
	 * method name   : init
	 * description : initialization
	 * @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 : Convert a string containing hexadecimal encoding into a string containing rare characters
	 * @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 : Convert a string containing rare characters into a string containing hexadecimal encoding
	 * @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 = "Wang Zhehao";
		name = cu.getHex(name);
		System.out.println("Cent2Dollar(1) = " + name);
		name = cu.getRareWord(name);
		System.out.println("Cent2Dollar(1) = " + name);

	}
}

 

 The txt file used in the code is shown in the attachment.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483628&siteId=291194637