After the java environment is installed, there is native2ascii.exe in the bin directory of jdk that can achieve similar functions, but the same function can also be achieved through java code.
String conversion unicode java method code snippet:
Copy the code code as follows:
/**
* String conversion to unicode
*/
public static String string2Unicode(String string) {
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
// Take out 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 snippet:
Copy the code code as follows:
/**
* unicode to 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 out each code point
int data = Integer.parseInt(hex[i], 16);
//Append to string
string.append((char) data);
}
return string.toString();
}
Test java code snippet:
Copy the code code as follows:
public static void main(String[] args) {
String test = "The most code website address: www.zuidaima.com";
String unicode = string2Unicode(test);
String string = unicode2String(unicode);
System.out.println(unicode);
System.out.println(string);
}
Output result:
/u6700/u4ee3/u7801/u7f51/u7ad9/u5730/u5740/u3a/u77/u77/u77/u2e/u7a/u75/u69/u64/u61/u69/u6d/u61/u2e/u63/u6f/u6d