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 outputFile, Map queries)
get Bytes
FileInputStream in = null;
try {
    in = new FileInputStream(outputFile);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int c;
    byte buffer[] = new byte[1024];
    while ((c = in.read(buffer)) != -1) {
        for (int i = 0; i < c; i++)
...
byte[]getBytes(String s)
Converts a string to it ISO-8859-1 byte sequence It is an workaround for variable initialisations outside of functions.
try {
    return s.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("Unsupported Encoding", e);
byte[]getBytes(String s)
Returns the bytes of the string in ENCODING_ISO_8859_1.
try {
    return s.getBytes(ENCODING_ISO_8859_1);
} catch (UnsupportedEncodingException e) {
    return s.getBytes();
byte[]getBytes(String s)
get Bytes
return getBytes(s, "UTF-8");
byte[]getBytes(String s)
Return a byte array corresponding to the given String.
try {
    return getBytes(s, System.getProperty("jna.encoding"));
} catch (UnsupportedEncodingException e) {
    return s.getBytes();
byte[]getBytes(String s)
Returns a byte-array representing the default encoding for a String.
try {
    return s.getBytes(DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("The JVM does not support the compiler's default encoding.", e);
byte[]getBytes(String s)
get Bytes
char[] chars = s.toCharArray();
int size = chars.length;
byte[] bytes = new byte[size];
for (int i = 0; i < size;)
    bytes[i] = (byte) chars[i++];
return bytes;
byte[]getBytes(String s, String charsetName)
get Bytes
if (s == null) {
    return null;
try {
    return s.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
byte[]getBytes(String s, String encoding)
Returns the contents of the given string as an array of bytes in the platform's default encoding.
try {
    return s.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
    return s.getBytes();
byte[]getBytes(String s, String encoding)
get Bytes
byte[] bytes = null;
if (s != null) {
    try {
        bytes = s.getBytes(encoding);
    } catch (UnsupportedEncodingException e) {
        bytes = s.getBytes();
return bytes;