给一个中文字符串 utf8编码后的 byte[] ,使用io流转成gbk编码的byte[],怎么弄
想俩小时了

解决方案 »

  1.   

    哈哈,我也不会,不过你不回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
      

  2.   

    初学,不知道是否正确,请同学们指教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");
      

  3.   

    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[]转化为字符串再转化成另一种编码的字节数组要简单的多