How to convert a Java String to an ASCII byte array?
How to convert a Java String to an ASCII byte array?
Solution 1:
Using the getBytes
method, giving it the appropriate Charset
(or Charset
name).
Example:
String s = "Hello, there.";
byte[] b = s.getBytes(StandardCharsets.US_ASCII);
If more control is required (such as throwing an exception when a character outside the 7 bit US-ASCII is encountered) then CharsetDecoder
can be used:
private static byte[] strictStringToBytes(String s, Charset charset) throws CharacterCodingException {
ByteBuffer x = charset.newEncoder().onMalformedInput(CodingErrorAction.REPORT).encode(CharBuffer.wrap(s));
byte[] b = new byte[x.remaining()];
x.get(b);
return b;
}
Before Java 7 it is possible to use: byte[] b = s.getBytes("US-ASCII");
. The enum StandardCharsets
, the encoder as well as the specialized getBytes(Charset)
methods have been introduced in Java 7.
Solution 2:
If you are a guava user there is a handy Charsets
class:
String s = "Hello, world!";
byte[] b = s.getBytes(Charsets.US_ASCII);
Apart from not hard-coding arbitrary charset name in your source code it has a much bigger advantage: Charsets.US_ASCII
is of Charset
type (not String
) so you avoid checked UnsupportedEncodingException
thrown only from String.getBytes(String)
, but not from String.getBytes(Charset)
.
In Java 7 there is equivalent StandardCharsets
class.
Solution 3:
There is only one character wrong in the code you tried:
Charset characterSet = Charset.forName("US-ASCII");
String string = "Wazzup";
byte[] bytes = String.getBytes(characterSet);
^
Notice the upper case "String". This tries to invoke a static method on the string class, which does not exist. Instead you need to invoke the method on your string instance:
byte[] bytes = string.getBytes(characterSet);