问题在代码中的注释中,我认为这是一种更简单的提问方式......
简单的问题,但我似乎找不到答案。我想将字符串转换为它的byte[]
(简单,String.getBytes()
)。然后我想将一个字节字符串(101011010101001
例如)转换为一个字节 [] 并获取它的字符串值(这也很容易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);