Home  >  Article  >  Web Front-end  >  Sample code on how to convert encoding into Chinese in html page

Sample code on how to convert encoding into Chinese in html page

黄舟
黄舟Original
2017-07-22 13:33:343851browse

Sample code on how to convert encoding into Chinese in html page

private static String convertToChinese(String dataStr) {
  System.out.println("--------data str---->" + dataStr); 
  if(dataStr == null || dataStr.length() == 0) {
   return dataStr;
  }
  int start = 0;
  int end = 0;
  final StringBuffer buffer = new StringBuffer();
  while (start > -1) {
   int system = 10;// 进制
   if (start == 0) {
    int t = dataStr.indexOf("&#");
    if (start != t)
     start = t;
    if(start > 0) {
     buffer.append(dataStr.substring(0, start));
    }
    if(start == -1) {
     return dataStr;
    }
   }
   end = dataStr.indexOf(";", start + 2);
   String charStr = "";
   if (end != -1) {
    charStr = dataStr.substring(start + 2, end);
    // 判断进制
    char s = charStr.charAt(0);
    if (s == 'x' || s == 'X') {
     system = 16;
     charStr = charStr.substring(1);
    }
    // 转换
    try {
     char letter = (char) Integer.parseInt(charStr, system);
     buffer.append(new Character(letter).toString());
    } catch (NumberFormatException e) {
     e.printStackTrace();
    }
   }
   
   // 处理当前unicode字符到下一个unicode字符之间的非unicode字符
   start = dataStr.indexOf("&#", end);
   if (start - end > 1) {
    buffer.append(dataStr.substring(end + 1, start));
   }
   // 处理最后面的非 unicode字符
   if (start == -1) {
    int length = dataStr.length();
    if (end + 1 != length) {
     buffer.append(dataStr.substring(end + 1, length));
    }
   }
  }
  return buffer.toString();
 }

The above is the detailed content of Sample code on how to convert encoding into Chinese in html page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn