日期:2014-05-17 浏览次数:20928 次
public class Test { /** * @param args */ public static void main(String[] args) { String str = "地区厞";//"地区厞" ; System.out.println(uniToASC(str)) ; System.out.println(ascii2String("地区厞")) ; } //转ascii public static String uniToASC(String unicode) { String output = ""; char[] charArray = unicode.toCharArray(); for (int i = 0; i < charArray.length; ++i) { char a = charArray[i]; if ((int) a > 255) { output += "&#" + (int) a + ";"; } else { output += a; } } return output; } public static char ascii2Char(int ASCII) { return (char) ASCII; } //ascII转中文 public static String ascii2String(String ASCIIs) { ASCIIs = ASCIIs.replace(";&#", ",") ; ASCIIs = ASCIIs.replace(";", "") ; ASCIIs = ASCIIs.replace("&#", "") ; System.out.println(ASCIIs) ; String[] ASCIIss = ASCIIs.split(","); StringBuffer sb = new StringBuffer(); for (int i = 0; i < ASCIIss.length; i++) { sb.append((char) ascii2Char(Integer.parseInt(ASCIIss[i]))); } return sb.toString(); } }