Using the getBytes
method, giving it the appropriate Charset
(or Charset
name).
Example:
String s = "Hello, there.";
byte[] b = s.getBytes(StandardCharsets.US_ASCII);
(Before Java 7: byte[] b = s.getBytes("US-ASCII");
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…