How to convert a Java String to an ASCII byte array?
Using the getBytes method, giving it the appropriate Charset (or Charset name).
Charset
Example:
String s = "Hello, there."; byte[] b = s.getBytes(StandardCharsets.US_ASCII);
(Before Java 7: byte[] b = s.getBytes("US-ASCII");)
byte[] b = s.getBytes("US-ASCII");