public static void main(String[] args) throws UnsupportedEncodingException {
byte[] bs = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
String s = new String(bs,3,4);
System.out.println(s);
}
打印出来怎么是乱码啊?如何打印出4567

解决方案 »

  1.   

    public static void main(String[] args) throws UnsupportedEncodingException {
    byte[] bs = new byte[] { 49, 50, 51, 52, 53, 54, 55, 56, 57 };
    String s = new String(bs, 3, 4);
    System.out.println(s);
    }
      

  2.   

    byte[] bs = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    改成
    byte[] bs = new byte[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    因为你那样写的不是字符'1',而是ascii为1的字符,所以打印出来是乱码。
      

  3.   

    byte字节数组转码,会两个byte转成一个字符