1

I want to convert a series of bytes in a byte[] to a string to append to a text file. Then when the string is pulled I want to convert it back to byte[] to decode it. I know I can't use getBytes(), I need to copy the string to it's original byte[] format then decode it back to a readable string. Is this possible?

     byte[] hash = pw.getBytes("UTF8");
     pw = hash.toString();

Then I pass the string to another class where I need it to be read as bytes, not changed to its byte form.

0

1 Answer 1

3

toString won't work. You need to write, because that is the string representation of Array Object.

String s = new String(hash,"UTF-8");
Sign up to request clarification or add additional context in comments.

5 Comments

So this is the correct way to turn the byte[] in to a String. How do I convert the String back in to bytes?
@caine1337: Exactly the way you did it in your original question, with pw.getBytes("UTF-8"). You've already done that correctly.
@caine1337 byte[] hash = pw.getBytes("UTF8"); where pw is a string.
Down votes, when helping ?? Great.
So the series of encoded characters in my String will be read by getBytes the same way that the original String would?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.