因为DB中的数据为blob,所以现在需要将String和Byte[]进行相互转换!发现一直是乱码,为了排除DB影响,自己写了个test
        String strTest = "123";
        byte[] test = strTest.getBytes();
        String result = String.valueOf(test);可是执行后result=“[B@1db04ed”;不知是什么原因导致的,望高手解答.....

解决方案 »

  1.   

    String strTest = "123";
            byte[] test = strTest.getBytes();
            String result = new String(test);
      

  2.   

    不要用valueof,用new
    String result=new String(test);
    如果还不行的话就不要用byte,用char就好了
    String teststr="sadfasdfasdfsdafasdfasdfef";
    char[] testchar=teststr.tocharArray();
    String result=new String(testchar);
      

  3.   

    String.valueOf(test);
    这个方法没有参数为byte数组的形式。
    直接构造
    String result=new String(test,"ISO-8859-1");最好这里也制定字符集:byte[] test = strTest.getBytes("ISO-8859-1");
      

  4.   

    提醒1楼和2楼:
    public String(byte[] bytes)
    这个构造方法因为依赖平台的默认字符集,可能出现一些奇怪的问题,最好不要使用。
      

  5.   

    String result = String.valueOf(test);改语句是把 test数组的地址转换成字符输出来而已,并不是把里面的内容转换。
    具体的转换方法,楼上的一些高手已经说明了。
      

  6.   

    import java.io.UnsupportedEncodingException;public class Str2Bytes { public static void main(String[] args) {
    String test = "测试字符串 for test";
    String charset = "UTF-8";
    byte[] bytes = null;
    try {
    bytes = test.getBytes(charset);
    String t = new String(bytes, charset);
    System.out.println(t);
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    }}
      

  7.   

    String strTest = "123";
            byte[] test = strTest.getBytes(“数据库的编码格式”);
            String result = String.valueOf(test,“你想要的编码格式”);eg:
    String strTest = "123";
            byte[] test = strTest.getBytes("iso-8859-1");
            String result = String.valueOf(test,"gb2312");
      

  8.   

    unicode => iso-8859-1 => else