Reprint: http://www.jb51.net/article/56096.htm
This article mainly introduces the Java string string and Unicode character conversion code, the need for friends can refer to the following
After the Java environment is installed, the JDK's Bin directory has a native2ascii.exe that can achieve similar functions, but it can also achieve the same functionality through Java code.
String Conversion Unicode Java method code fragment:
Copy code code as follows:
/**
* String Conversion Unicode
*/
public static string String2unicode (String string) {
StringBuffer Unicode = new StringBuffer ();
for (int i = 0; i < string.length (); i++) {
Remove each character
char C = String.charat (i);
Convert to Unicode
Unicode.append ("\\u" + integer.tohexstring (c));
}
return unicode.tostring ();
}
Unicode conversion string Java method code fragment:
Copy code code as follows:
/**
* Unicode Spin string
*/
public static string Unicode2string (string Unicode) {
StringBuffer string = new StringBuffer ();
string[] hex = unicode.split ("\\\\u");
for (int i = 1; i < hex.length; i++) {
Convert every code point
int data = Integer.parseint (Hex[i], 16);
Append As String
String.append ((char) data);
}
return string.tostring ();
}
To test a Java code fragment:
Copy code code as follows:
public static void Main (string[] args) {
String test = "Most code Web site address: www.zuidaima.com";
String Unicode = string2unicode (test);
String string = Unicode2string (Unicode);
SYSTEM.OUT.PRINTLN (Unicode);
System.out.println (string);
}
Output results:
\u6700\u4ee3\u7801\u7f51\u7ad9\u5730\u5740\u3a\u77\u77\u77\u2e\u7a\u75\u69\u64\u61\u69\u6d\u61\u2e\u63\u6f\u6d