日期:2014-05-17 浏览次数:21476 次
/** * 字符串转化为unicode * @param gbString * @return */ public static String encodeUnicode(final String gbString) { char[] utfBytes = gbString.toCharArray(); String unicodeBytes = ""; for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) { String hexB = Integer.toHexString(utfBytes[byteIndex]); if (hexB.length() <= 2) { hexB = "00" + hexB; } //unicodeBytes = unicodeBytes + "\\u" + hexB; unicodeBytes = unicodeBytes + hexB; } System.out.println(unicodeBytes); return unicodeBytes; } // unicode转化汉字 public static StringBuffer decodeUnicode(String utfStr) { final StringBuffer buffer = new StringBuffer(); String charStr = ""; String operStr = utfStr; for(int i =0 ; i < utfStr.length() ;i=+4){ charStr = operStr.substring(0, 4); operStr = operStr.substring(4, operStr.length()); char letter = (char) Integer.parseInt(charStr, 16); buffer.append(new Character(letter).toString()); } return buffer; }