How do you convert a string to a byte array and visa versa?
Java.lang.String has certain utilities to convert a string into a byte array and visa versa. A string can be converted correctly and fully into a byte array by using a character encoding such as UTF-8, US-ASCII. All the overloaded String.getBytes() methods will convert a string into a byte array. Some of these methods take an encoding identifier as an argument, while others take no arguments and will use the platform’s default encoding to convert the string. A byte array can be converted into a string using the string constructor methods. Some take an encoding identifier, while others take none and use the platform’s default encoding. A listing of appropriate encoding identifiers is found at Character Encodings. The following program does string to byte array conversion using the default platform encoding, and the UTF-8 character encoding.