问题在于代码 < / strong> 中的评论,
简单问题, 但我似乎找不到一个答案 。 我想将字符串转换为 s byte []
(easy, String.getBytes ()
) 。 然后我想将字节的字符串( 1011011010101010001
等)转换为字节 [], 并获得字节的字符串值( 也很简单 : new String(byte)
)
这是我到现在为止得到的:
Scanner scan = new Scanner(System.in);
String string = scan.nextLine();
String byteString = "";
for (byte b : string.getBytes()) {
byteString += b;
}
System.out.println(byteString);
//This isn t exactly how it works, these two parts in separate methods, but you get the idea...
String byteString = scan.nextLine();
byte[] bytes = byteString.literalToBytes() //<== or something like that...
//The line above is pretty much all I need...
String string = new String(bytes);
System.out.println(string);