您的位置:首页 > 其它

将unicode编码转成中文

2017-08-03 10:55 162 查看
解决方法1:

   利用java自身封装的方法进行转码,代码如下:

  public static void main ( String[] args ) throws UnsupportedEncodingException{
String str = "\u6211\u7231\u4e2d\u534e";
String str2=URLDecoder.decode(str,"utf-8");
System.out.println(str2);

}
解决方法2:

  如果上述方法不能解决,也可以用如下方法进行转码:

  private static String ascii2native ( String asciicode ){
String[] asciis = asciicode.split ("\\\\u");
String nativeValue = asciis[0];
try{
for ( int i = 1; i < asciis.length; i++ ){
String code = asciis[i];
nativeValue += (char) Integer.parseInt (code.substring (0, 4), 16);
if (code.length () > 4){
nativeValue += code.substring (4, code.length ());
}
}
}catch (NumberFormatException e){
return asciicode;
}
return nativeValue;
}

public static void main ( String[] args ) throws UnsupportedEncodingException{
String str = "\u6211\u7231\u4e2d\u534e";
String result = ascii2native (str);
System.out.println(str);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息