Java实现 中文转换成Unicode编码 和 Unicode编码转换成中文
想要实现中文字符转换为Unicode编码的话主要用到的是一个这样的包,自己可以去API文档里面查看下的
java.util.Properties;
直接进入主题吧,主要是
1 package Test01; 2 3 import java.util.Properties; 4 public class Test { 5 public static void main(String[] args) { 6 String s = "简介"; 7 String tt = gbEncoding(s); 8 System.out.println(decodeUnicode("\\u7b80\\u4ecb")); 9 System.out.println(HTMLDecoder.decode("开始")); 10 String s1 = "\u7b80\u4ecb"; 11 System.out.println(s.indexOf("\\")); 12 } 13 public static String gbEncoding(final String gbString) { 14 char[] utfBytes = gbString.toCharArray(); 15 String unicodeBytes = ""; 16 for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) { 17 String hexB = Integer.toHexString(utfBytes[byteIndex]); 18 if (hexB.length() <= 2) { 19 hexB = "00" + hexB; 20 } 21 unicodeBytes = unicodeBytes + "\\u" + hexB; 22 } 23 System.out.println("unicodeBytes is: " + unicodeBytes); 24 return unicodeBytes; } 25 public static String decodeUnicode(final String dataStr) { 26 int start = 0; 27 int end = 0; 28 final StringBuffer buffer = new StringBuffer(); 29 while (start > -1) { 30 end = dataStr.indexOf("\\u", start + 2); 31 String charStr = ""; 32 if (end == -1) { 33 charStr = dataStr.substring(start + 2, dataStr.length()); 34 } else { 35 charStr = dataStr.substring(start + 2, end); 36 } 37 char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。 38 buffer.append(new Character(letter).toString()); 39 start = end; 40 } 41 return buffer.toString(); 42 } 43 }
我就是我,是不一样的烟火...