给一个中文字符串 utf8编码后的 byte[] ,使用io流转成gbk编码的byte[],怎么弄 给一个中文字符串 utf8编码后的 byte[] ,使用io流转成gbk编码的byte[],怎么弄想俩小时了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哈哈,我也不会,不过你不回google啊?http://www.google.com/search?q=java+utf8%E7%BC%96%E7%A0%81++gb2312&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:zh-CN:official&client=firefox-a 初学,不知道是否正确,请同学们指教Byte abytUtf8[]=你的utf字符串;InputStreamReader isr=new InputStreamReader(new ByteArrayInputStream(abytUtf8),"utf-8")StringBuffer sb=new StringBuffer();int intChar;while ((intChar=isr.read())!=-1){ sb.append((char)intChar);}isr.close();Byte abytGbk[]=sb.toString().getBytes("gbk"); public static void main(String[] args){ String s="非标加进橱架叶"; //原字符串 try { byte[] b=s.getBytes("utf-8"); //u t f-8编码的byte[] //将b作为输入流读取字符 InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(b),"utf-8"); //创建一个输出到byte[]数组的输出流 ByteArrayOutputStream baout = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(baout, "GBK"); //将输入流中的u t f-8编码的字节流以GBK编码输出; int c; while((c=reader.read())!=-1){ writer.write(c); } writer.close(); reader.close(); //获取输出的byte[]; byte[] b1=baout.toByteArray(); //验证转换后在字符串上是否保持一致 System.out.println(new String(b1,"GBK")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }这样不知是否符合楼主以IO流转换编码的目的.其它如果只是为了获取转换编码后的数组的话将byte[]转化为字符串再转化成另一种编码的字节数组要简单的多 将只包含字母的单词放到String数组中,将数字放到int数组中。 刚学正则表达式求助。 window下可以,linux就是连不上mysql SimpleDateFormat问题 java问题,编译时不能带包? 请教高人:如何在UDP数据报中传输及显示中文信息? 新手,求助,SQL数据库中表输出问题 如何在网站服务器端取连接的客户端机器的主板信息?UP有分 请问java中有没有一个类似于messagebox或alert的API? 新手求解怎么解决下面这个异常啊 大虾帮忙啊~~~~~~~~~~~~··· 关于java观感的问题
InputStreamReader isr=new InputStreamReader(new ByteArrayInputStream(abytUtf8),"utf-8")
StringBuffer sb=new StringBuffer();
int intChar;
while ((intChar=isr.read())!=-1){
sb.append((char)intChar);
}
isr.close();
Byte abytGbk[]=sb.toString().getBytes("gbk");
String s="非标加进橱架叶"; //原字符串
try {
byte[] b=s.getBytes("utf-8"); //u t f-8编码的byte[]
//将b作为输入流读取字符
InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(b),"utf-8");
//创建一个输出到byte[]数组的输出流
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(baout, "GBK");
//将输入流中的u t f-8编码的字节流以GBK编码输出;
int c;
while((c=reader.read())!=-1){
writer.write(c);
}
writer.close();
reader.close();
//获取输出的byte[];
byte[] b1=baout.toByteArray();
//验证转换后在字符串上是否保持一致
System.out.println(new String(b1,"GBK"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}这样不知是否符合楼主以IO流转换编码的目的.
其它如果只是为了获取转换编码后的数组的话将byte[]转化为字符串再转化成另一种编码的字节数组要简单的多