Java implements Chinese string and unicode mutual conversion tool class

  • 2021-07-22 09:39:06
  • OfStack

In this paper, we share the specific code of Java to realize the mutual conversion between Chinese string and unicode for your reference. The specific contents are as follows

The principle uses java to realize escape and unescape functions of js.


/**
 *  Chinese string and unicode Interchange tool class  <br>
 * 
 * @author hkb <br>
 */
public class UnicodeConvertUtils {

  /**
   *  Realization js Adj. escape Function 
   * 
   * @param input
   *       String to be passed in 
   * @return
   */
  public static String escape(String input) {
    int len = input.length();
    int i;
    char j;
    StringBuffer result = new StringBuffer();
    result.ensureCapacity(len * 6);
    for (i = 0; i < len; i++) {
      j = input.charAt(i);
      if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j)) {
        result.append(j);
      } else if (j < 256) {
        result.append("%");
        if (j < 16) {
          result.append("0");
        }
        result.append(Integer.toString(j, 16));
      } else {
        result.append("%u");
        result.append(Integer.toString(j, 16));
      }
    }
    return result.toString();

  }

  /**
   *  Realization js Adj. unescape Function 
   * 
   * @param input
   *       String to be passed in 
   * @return
   */
  public static String unescape(String input) {
    int len = input.length();
    StringBuffer result = new StringBuffer();
    result.ensureCapacity(len);
    int lastPos = 0, pos = 0;
    char ch;
    while (lastPos < len) {
      pos = input.indexOf("%", lastPos);
      if (pos == lastPos) {
        if (input.charAt(pos + 1) == 'u') {
          ch = (char) Integer.parseInt(input.substring(pos + 2, pos + 6), 16);
          result.append(ch);
          lastPos = pos + 6;
        } else {
          ch = (char) Integer.parseInt(input.substring(pos + 1, pos + 3), 16);
          result.append(ch);
          lastPos = pos + 3;
        }
      } else {
        if (pos == -1) {
          result.append(input.substring(lastPos));
          lastPos = len;
        } else {
          result.append(input.substring(lastPos, pos));
          lastPos = pos;
        }
      }
    }
    return result.toString();
  }

  /**
   * unicode Translate to Chinese 
   * 
   * @param input
   *       String to be passed in 
   * @return
   */
  public static String toGb2312(String input) {
    input = input.trim().replaceAll("(?i)\\\\u", "%u");
    return unescape(input);
  }

  /**
   *  Chinese string conversion unicode
   * 
   * @param input
   *       String to be passed in 
   * @return
   */
  public static String toUnicode(String input) {
    input = input.trim();
    String output = escape(input).toLowerCase().replace("%u", "\\u");
    return output.replaceAll("(?i)%7b", "{").replaceAll("(?i)%7d", "}").replaceAll("(?i)%3a", ":")
        .replaceAll("(?i)%2c", ",").replaceAll("(?i)%27", "'").replaceAll("(?i)%22", "\"")
        .replaceAll("(?i)%5b", "[").replaceAll("(?i)%5d", "]").replaceAll("(?i)%3D", "=")
        .replaceAll("(?i)%20", " ").replaceAll("(?i)%3E", ">").replaceAll("(?i)%3C", "<")
        .replaceAll("(?i)%3F", "?").replaceAll("(?i)%5c", "\\");
  }

  /**
   *  Test 
   * 
   * @param args
   */
  public static void main(String[] args) {
    System.out.println(toUnicode(" How do you do "));
    System.out.println(toGb2312("\u4f60\u597d"));
    //  Equivalent to the above 
    System.out.println(toGb2312("\\u4f60\\u597d"));
  }
}

Related articles: