Conversion of Java String to Byte Array
Question:
How can a Java String be converted into a byte array?
Answer:
To convert a Java String to a byte array, utilize one of the following methods:
Displaying Byte Array:
The default toString() method of a byte array doesn't provide a useful representation. To view the byte array in a human-readable format, use:
Retrieving String from Byte Array:
To convert a byte array back to a String, use:
Note on Character Encoding:
It's essential to understand the character encoding used when converting between strings and byte arrays. Strings in Java are internally stored using UTF-16. Using different character encodings (e.g., ASCII, UTF-8) will result in different byte array representations for the same glyphs.
The above is the detailed content of How Can I Convert a Java String to a Byte Array and Back?. For more information, please follow other related articles on the PHP Chinese website!