用java如何实现gb2312和utf-8之间的转换 = new String(msg.getBytes(utf-8), "GB2312"); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class Test{ public static void main(String[] args) throws Exception{ String s = "用java如何实现gb2312和utf-8之间的转换"; byte[] gb2312 = s.getBytes("GB2312"); for(int i=0;i<gb2312.length;i++) System.out.print(gb2312[i]+","); System.out.println(); s = new String(gb2312,"GB2312"); byte[] utf8 = s.getBytes("UTF-8"); for(int i=0;i<utf8.length;i++) System.out.print(utf8[i]+","); System.out.println("\n"+new String(utf8,"UTF-8")); }} 搂主,送你连个有用的函数(要先import java.io.IOException;): public String toUTF(String str) throws IOException { str = new String(str.getBytes("gb2312"),"ISO-8859-1"); return str; } public String toGB(String str) throws IOException { str = new String(str.getBytes("ISO-8859-1"),"gb2312"); return str; } 使用方法:GB to UTF:String gbstr="abc";String utfstr=toUTF(gbstr);UTF to GB:String utfstr="abc";String gbstr=toGB(utfstr); 送你一个转换类:public class ExChinese { public ExChinese() { } public String ex_ch(String str) { if(str==null) { str =""; } else { try { str = new String(str.getBytes("iso-8859-1"),"gb2312"); } catch (Exception ex) { ex.printStackTrace(); } } return str ; }} 帮忙测试下代码。。。 C原型,如何在JAVA中声明,引用? 怎么重画JInternalFrame java 里定义这个类怎么理解? 求助java读取excel文件,再写到数据库中遇到了问题! 关于java2的两个问题 推荐有关java的书 在Applet中如何让可编辑的 JComboBox 响应键盘、鼠标、焦点等事件? jar 文件什么用 java VM 运行? 一个connection pool 问题 新来的,来报个道,希望大家多帮帮; ) 请问怎么连接数据库??
public static void main(String[] args) throws Exception{
String s = "用java如何实现gb2312和utf-8之间的转换";
byte[] gb2312 = s.getBytes("GB2312");
for(int i=0;i<gb2312.length;i++)
System.out.print(gb2312[i]+",");
System.out.println();
s = new String(gb2312,"GB2312");
byte[] utf8 = s.getBytes("UTF-8");
for(int i=0;i<utf8.length;i++)
System.out.print(utf8[i]+",");
System.out.println("\n"+new String(utf8,"UTF-8"));
}
}
{
str = new String(str.getBytes("gb2312"),"ISO-8859-1");
return str;
} public String toGB(String str) throws IOException
{
str = new String(str.getBytes("ISO-8859-1"),"gb2312");
return str;
}
GB to UTF:String gbstr="abc";
String utfstr=toUTF(gbstr);UTF to GB:String utfstr="abc";
String gbstr=toGB(utfstr);
public class ExChinese {
public ExChinese() {
}
public String ex_ch(String str)
{
if(str==null)
{
str ="";
}
else
{
try
{
str = new String(str.getBytes("iso-8859-1"),"gb2312");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return str ;
}
}