将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);
}
利用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);
}
相关文章推荐
- eclipse中 XXXX.properties文件 不显示中文,显示的unicode编码。
- Css中常用中文字体的Unicode编码对照
- OS NSDictionary 输出中文字符”乱码”(Unicode编码)问题?
- 中文和Unicode编码互转
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
- [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
- 中文转换成Unicode编码 和 Unicode编码转换为中文
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
- Unicode中文和特殊字符的编码范围
- 查看中文字符的unicode编码
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
- 解决 iOS NSDictionary 输出中文字符”乱码”(Unicode编码)问题
- java中国际化properties的Unicode编码文件用命令转化为中文文件
- 关于eclipse中properties文件中文提交到SVN后变成unicode编码
- Unicode中文和特殊字符的编码范围 及部分正则
- Foundation+Log.m,让网络请求回来的中文被unicode编码的数据汉字显示
- 中文在unicode中的编码范围
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030(转)
- NSString值为Unicode格式的字符串编码转换成中文编码
- python编码转换,列表编码转换,编码检测,pycharm编写python乱码,文件中以unicode编码方式转成中文