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

Java实现中文字符串与unicode互转工具类

程序员文章站 2023-12-12 15:47:16
本文实例为大家分享了java实现中文字符串与unicode互转的具体代码,供大家参考,具体内容如下 原理利用了java实现js的escape以及unescape函数。...

本文实例为大家分享了java实现中文字符串与unicode互转的具体代码,供大家参考,具体内容如下

原理利用了java实现js的escape以及unescape函数。

/**
 * 中文字符串和unicode互转工具类 <br>
 * 
 * @author hkb <br>
 */
public class unicodeconvertutils {

  /**
   * 实现js的escape函数
   * 
   * @param input
   *      待传入字符串
   * @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();

  }

  /**
   * 实现js的unescape函数
   * 
   * @param input
   *      待传入字符串
   * @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转中文
   * 
   * @param input
   *      待传入字符串
   * @return
   */
  public static string togb2312(string input) {
    input = input.trim().replaceall("(?i)\\\\u", "%u");
    return unescape(input);
  }

  /**
   * 中文字符串转unicode
   * 
   * @param input
   *      待传入字符串
   * @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", "\\");
  }

  /**
   * 测试
   * 
   * @param args
   */
  public static void main(string[] args) {
    system.out.println(tounicode("你好"));
    system.out.println(togb2312("\u4f60\u597d"));
    // 等同于上面
    system.out.println(togb2312("\\u4f60\\u597d"));
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: