因为DB中的数据为blob,所以现在需要将String和Byte[]进行相互转换!发现一直是乱码,为了排除DB影响,自己写了个test
String strTest = "123";
byte[] test = strTest.getBytes();
String result = String.valueOf(test);可是执行后result=“[B@1db04ed”;不知是什么原因导致的,望高手解答.....
String strTest = "123";
byte[] test = strTest.getBytes();
String result = String.valueOf(test);可是执行后result=“[B@1db04ed”;不知是什么原因导致的,望高手解答.....
byte[] test = strTest.getBytes();
String result = new String(test);
String result=new String(test);
如果还不行的话就不要用byte,用char就好了
String teststr="sadfasdfasdfsdafasdfasdfef";
char[] testchar=teststr.tocharArray();
String result=new String(testchar);
这个方法没有参数为byte数组的形式。
直接构造
String result=new String(test,"ISO-8859-1");最好这里也制定字符集:byte[] test = strTest.getBytes("ISO-8859-1");
public String(byte[] bytes)
这个构造方法因为依赖平台的默认字符集,可能出现一些奇怪的问题,最好不要使用。
具体的转换方法,楼上的一些高手已经说明了。
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();
}
}}
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");