Java Utililty Methods String to Byte Array

List of utility methods to do String to Byte Array

Description

The list of methods to do String to Byte Array are organized into topic(s).

Method

byte[]getBytes(String str)
Get ascii byte array for the string provided
try {
    return str.getBytes("US-ASCII");
} catch (UnsupportedEncodingException impossible) {
    return null;
byte[]getBytes(String str)
Should always be able convert to/from UTF-8, so encoding exceptions are converted to an Error to avoid adding throws declarations in lots of methods.
try {
    return str.getBytes("UTF8");
} catch (java.io.UnsupportedEncodingException e) {
    throw new Error("String to UTF-8 conversion failed: " + e.getMessage());
byte[]getBytes(String str)
get Bytes
return str.getBytes(CHARSET);
byte[]getBytes(String str)
Get the bytes with UTF-8
if (str == null) {
    return null;
try {
    return str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
return null;
byte[]getBytes(String str)
get Bytes
if (str != null) {
    try {
        return str.getBytes(CHARSET_NAME);
    } catch (UnsupportedEncodingException e) {
        return null;
} else {
    return null;
...
byte[]getBytes(String str)
get Bytes
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StringTokenizer st = new StringTokenizer(str, "-", false);
while (st.hasMoreTokens()) {
    int i = Integer.parseInt(st.nextToken());
    bos.write((byte) i);
return bos.toByteArray();
byte[]getBytes(String str)
get Bytes
byte[] bytes = str.getBytes();
try {
    bytes = str.getBytes("UTF-8");
} catch (UnsupportedEncodingException use) {
    LOGGER.severe("Exception: " + use.getMessage());
return bytes;
byte[]getBytes(String str, String charset)
get Bytes
try {
    return str.getBytes(charset);
} catch (UnsupportedEncodingException ex) {
    throw new IllegalArgumentException(charset, ex);
byte[]getBytes(String string, String charsetName)
get Bytes
try {
    return string.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    return null;
byte[]getBytes(String string, String encoding)
Translate the string to bytes using the given encoding
try {
    return string.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
    throw new IllegalArgumentException(encoding + " is not a known encoding name.", e);