请问大家,UTF-8如何转换成汉字 比如:人 这个汉字,它的UTF-8编码为:E4 BA BA ,那么我怎么把这几个字符,转换成:"人"这个汉字呢.大家帮帮我吧. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String utf;...String gbk=new String(utf.getBytes("utf-8"),"gb2312"); 写了三种不同的方法,楼主可以参考一下:import java.io.UnsupportedEncodingException;import java.net.URLDecoder;public class Test { public static void main(String[] args) throws UnsupportedEncodingException { String str = "40 D0 BF E4 BA BA 35"; // 方法一:采用 URLDecoder.decode() 转换浏览器地址栏中的方法 String newStr = URLDecoder.decode("%" + str.replace(" ", "%"), "utf-8"); System.out.println(newStr); // 方法二:将字符序列转为 byte 数组 String[] strs = str.split(" "); byte[] b = new byte[strs.length]; for(int i=0; i<strs.length; i++){ b[i] = (byte)Integer.parseInt(strs[i], 16); } newStr = new String(b, "utf-8"); System.out.println(newStr); // 方法三:按照 UTF-8 的编码格式,直接解析字符串 strs = str.split(" "); StringBuffer sb = new StringBuffer(); for(int i=0; i<strs.length; i++){ int n1 = Integer.parseInt(strs[i], 16); if ( (n1 >> 5) == 0x6 ) { int n2 = Integer.parseInt(strs[++i], 16); n1 = (n1 & 0x1F) << 6; n2 = n2 & 0x3F; n1 |= n2; } else if ( (n1 >> 4) == 0xE) { int n2 = Integer.parseInt(strs[++i], 16); int n3 = Integer.parseInt(strs[++i], 16); n1 = (n1 & 0x0F) << 12; n2 = (n2 & 0x3F) << 6; n3 = (n3 & 0x3F); n1 = n1 | n2 | n3; } sb.append((char)n1); } System.out.println(sb.toString()); }} java Swing实现类似UML工具拖动一个控件然后生成并可以添加监听事件 一个关于eclipse中RCP delta pack的使用问题 这个类有什么问题?? list的copy问题 为什么JAVA API文档中老是出现<T>是类似C++中的模板吗?是的话在JAVA中要怎么理解 请问如何动态地创建一个对象? 安装了1.4.02_03的j2sdk,请问如何查看每个类包的详细信息 怎样改变菜单的默认行为? 借着人气,请大家帮我看看访问Access异常问题,多谢~! 关于图形颜色的问题 关于UltraEdit的筛选同一后缀名文件 如何保护代码
...
String gbk=new String(utf.getBytes("utf-8"),"gb2312");
import java.net.URLDecoder;public class Test { public static void main(String[] args) throws UnsupportedEncodingException { String str = "40 D0 BF E4 BA BA 35"; // 方法一:采用 URLDecoder.decode() 转换浏览器地址栏中的方法
String newStr = URLDecoder.decode("%" + str.replace(" ", "%"), "utf-8");
System.out.println(newStr); // 方法二:将字符序列转为 byte 数组
String[] strs = str.split(" ");
byte[] b = new byte[strs.length];
for(int i=0; i<strs.length; i++){
b[i] = (byte)Integer.parseInt(strs[i], 16);
}
newStr = new String(b, "utf-8");
System.out.println(newStr);
// 方法三:按照 UTF-8 的编码格式,直接解析字符串
strs = str.split(" ");
StringBuffer sb = new StringBuffer();
for(int i=0; i<strs.length; i++){
int n1 = Integer.parseInt(strs[i], 16);
if ( (n1 >> 5) == 0x6 ) {
int n2 = Integer.parseInt(strs[++i], 16);
n1 = (n1 & 0x1F) << 6;
n2 = n2 & 0x3F;
n1 |= n2;
} else if ( (n1 >> 4) == 0xE) {
int n2 = Integer.parseInt(strs[++i], 16);
int n3 = Integer.parseInt(strs[++i], 16);
n1 = (n1 & 0x0F) << 12;
n2 = (n2 & 0x3F) << 6;
n3 = (n3 & 0x3F);
n1 = n1 | n2 | n3;
}
sb.append((char)n1);
}
System.out.println(sb.toString());
}
}