可能重复:
Java 字节数组到字符串到字节数组
我有一个名为 READ() 的方法,它接受一个字符串参数。该字符串已经被转换为字节。我想要的只是转换成可读的字符串。
public static String READ(final String data) throws UnsupportedEncodingException{
char[] temp = data.toCharArray();
byte[] bytes = new byte[temp.length];
int i = 0;
for(char c : temp){
bytes[i++] = (byte)c;
}
return new String(bytes, "UTF-8");
}
public static String SEND(String data) throws UnsupportedEncodingException{
return data.getBytes()+"";
}
测试:
String msg = "testing !";
String msgBytes = null;
try {
msgBytes = SEND(msg);
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
System.out.println( "SEND: " + msgBytes);
try {
System.out.println("RECEIVE: " + READ(msgBytes));
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
输出是:
发送:[B@452467ec
接收:[B@452467ec