[Solved] how do I print the contents of an array of bytes as bytes? [duplicate]


getBytes() doesn’t change the string, it encodes string into a sequence of bytes using the platform’s default charset.

In order to print the array of bytes as a String value,

 String s = new String(bytes);

Edit:

it seems as you want to print the string as bytes, for which you can use

Arrays.toString(bytes)

See this code,

String yourString = "This is an example text";
byte[] bytes = yourString.getBytes();
String decryptedString = new String(bytes);
System.out.println("Original String from bytes: " + decryptedString);
System.out.println("String represented as bytes : " + Arrays.toString(bytes));

Output,

Original String from bytes: This is an example text
String represented as bytes : [84, 104, 105, 115, 32, 105, 115, 32, 97, 110, 32, 101, 120, 97, 109, 112, 108, 101, 32, 116, 101, 120, 116]

4

solved how do I print the contents of an array of bytes as bytes? [duplicate]