package com.cmh.test;
public class Test {
public static void main(String[] args) {
String str = "123456789";
byte[] myByte = str.getBytes();
String byte2Str = myByte.toString();
StringBuffer sbuf = new StringBuffer(myByte.length);
if(myByte != null) {
for(int i = 0; i<myByte.length; i++) {
sbuf.append(String.valueOf((char)myByte[i]));
}
}
System.err.println("myByte==="+myByte);
System.err.println("byte2Str==="+byte2Str);
System.err.println("sbuf==="+sbuf);
}
}
运行结果:
myByte===[B@7d6e7d6e
byte2Str===[B@7d6e7d6e
sbuf===123456789
本文演示了如何在Java中将一个字符串转换为Byte数组,并通过不同方式将其还原回原始字符串的过程。首先使用标准的getBytes方法进行转换,然后探讨了如何通过StringBuffer将Byte数组重新构建为字符串。
3万+

被折叠的 条评论
为什么被折叠?



