public class Test { public static void main(String[] args) { String tt ="\u8bd5\u56fe\u521b\u5efa\u6587\u4ef6"; System.out.println( tt.toCharArray()); } }
public class Test { public static void main(String[] args) { String tt ="\u8bd5\u56fe\u521b\u5efa\u6587\u4ef6"; System.out.println( tt); } }
但是如何封装成函数呢? 如 public String ToChinese(String str){ return ??; }
String tt ="\u8bd5\u56fe\u521b\u5efa\u6587\u4ef6";
System.out.println( tt.toCharArray());
}
}
String tt ="\u8bd5\u56fe\u521b\u5efa\u6587\u4ef6";
System.out.println( tt);
}
}
如 public String ToChinese(String str){
return ??;
}
String tt = str
return tt;
}
import java.util.*;
public class Unicode { public static void main(String[] args) {
String str=new String("试图创建文件");
String s;
try
{
byte[] by = str.getBytes("Unicode");
for(int i=2;i<by.length;i++)
{
s = Integer.toHexString(by[i]);
s = s.length() > 2 ? s.substring(6,8):s;
if(s.length()<2)
{
s = "0"+s;
}
System.out.println(s);
}
}
catch(Exception e)
{
}
}
}
自己改变一下顺序
* 将含unicode的字符串转为汉字字符串
* @param str String: 要转换的字符串(可能含有unicode字符)
* @return String: 转后的串
*/
public static String UnicodeToGB2312(String str) { String res = null;
StringBuffer sb = new StringBuffer();
try {
while (str.length() > 0) {
if (str.startsWith("\\u")) {
int x = Integer.parseInt(str.substring(2, 6), 16);
sb.append( (char) x);
str = str.substring(6);
}
else {
sb.append(str.charAt(0));
str = str.substring(1);
}
}
res = sb.toString();
}
catch (Exception e) {
System.err.println("in UnicodeToGB2312:" + e.toString());
}
return res;
}