JAVA里面关于byte数组和String之间的转换有关问题

JAVA里面关于byte数组和String之间的转换问题
JAVA里面关于byte数组和String之间的转换问题

把byte转化成string,必须经过编码。

例如下面一个例子:

import java.io.UnsupportedEncodingException;

public class test{
public static void main(String g[]) {
  String s = "12345abcd";
  byte b[] = s.getBytes();
  String t = b.toString();

  System.out.println(t);

}
}
输出字符串的结果和字符串s不一样了.

经过以下方式转码就可以正确转换了:

public class test{
public static void main(String g[]) {
  String s = "12345abcd";
  byte b[] = s.getBytes();
  try {
   String t = new String(b);
   System.out.print(t);
  } catch (Exception e) {
   e.printStackTrace();
  }
}
}