java 把 byte[] 数组转化为String
2012-05-03 15:32
537 查看
文章出处:http://www.mkyong.com/java/how-do-convert-byte-array-to-string-in-java/
In some cases, we have to convert String variable into a Byte array format, for example, JCE encryption. However how do weconvert a Byte[] array to String afterward?
Simple toString() function like following code is not working property. It will not display the original text but byte value.
In order to convert Byte array into String format correctly, we have to explicitly create a String object and assign the Byte array to it.
Text [Byte Format] : [B@187aeca
Text [Byte Format] : [B@187aeca
Text Decryted : This is an example
In some cases, we have to convert String variable into a Byte array format, for example, JCE encryption. However how do weconvert a Byte[] array to String afterward?
Simple toString() function like following code is not working property. It will not display the original text but byte value.
String s = bytes.toString();
In order to convert Byte array into String format correctly, we have to explicitly create a String object and assign the Byte array to it.
String s = new String(bytes);
public class TestByte
{
public static void main(String[] argv) {
String example = "This is an example";
byte[] bytes = example.getBytes();
System.out.println("Text : " + example);
System.out.println("Text [Byte Format] : " + bytes);
System.out.println("Text [Byte Format] : " + bytes.toString());
//String s = new String(bytes, "UTF-8");
String s = new String(bytes);System.out.println("Text Decryted : " + s);
}
}
Output
Text : This is an exampleText [Byte Format] : [B@187aeca
Text [Byte Format] : [B@187aeca
Text Decryted : This is an example
相关文章推荐
- java的对象-》jason-》String-》byte数组的转化方法。
- JAVA的数组 && 原始类型和引用类型以及String与int long相互转化 && String与byte[]相互转化
- 【Java】ArrayList<String>转化为String数组问题
- 读取Java文件到byte数组的三种方式及Java文件操作大全(包括文件加密,String加密)
- 将一个包含ASCII编码Unicode编码字符的Byte数组转化为一个完整的String
- java byte数组与String互转
- 几种字符串到byte[] 数组转化为String 的方法
- Java byte[]字节数组转换为String字符串的注意事项
- java8 利用 stream,lambda 将一个数组转化为字符串String
- 将Byte数组转化为String
- C# double float int string 与 byte数组 相互转化
- 几种字符串到byte[] 数组转化为String 的方法
- [转贴]将Byte数组转化为String
- 【Java】ArrayList<String>转化为String数组
- java中把对象转化为byte数组的方法
- Java String byte数组 字符集转换
- java中InputStream转化为byte[]数组
- 将Byte数组转化为String
- Java byte数组按位转化为long类型
- java里面byte数组和String字符串怎么转换