how to convert byte data which is stored in string variable to string

I have converted a string into byte in java and stored that byte data into a string variable.

String s = "anu";
byte[] d = s.getBytes();
String e = Arrays.toString(d);

How can I convert that string (in this case variable 'e') again into the same string assigned to s i.e."anu";

Jon Skeet
people
quotationmark

Firstly, I'd strongly advise against using getBytes() without a charset parameter.

Next, just use the string constructor that takes a byte array and a charset:

String s = "anu";
byte[] d = s.getBytes(StandardCharsets.UTF_8);
String e = new String(d, StandardCharsets.UTF_8);

Note that you should only do this for data which is inherently text to start with. If you've actually started with arbitrary binary data (e.g. an image, or the result of compression or encryption) you should use base64 or hex to represent it in text, otherwise you're very likely to lose data.

people

See more on this question at Stackoverflow