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[]asBytes(String basicString)
Get the corresponding byte array for a basic string.
final byte[] b = new byte[basicString.length()];
for (int i = 0; i < b.length; ++i) {
    b[i] = (byte) basicString.charAt(i);
return b;
byte[]asBytes(String hex)
as Bytes
if (hex.indexOf(' ') >= 0) {
    hex = hex.replaceAll(" ", "");
if (hex.startsWith("0x")) {
    hex = hex.substring(2);
return hexToBytes(hex);
byte[]asBytes(String hexStr)
as Bytes
if (hexStr.length() < 1) {
    return null;
byte[] result = new byte[hexStr.length() / 2];
int high, low;
for (int i = 0; i < hexStr.length() / 2; i++) {
    high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
    low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
...
byte[]convertStringToByte(String input)
convert String To Byte
return input == null ? new byte[0] : input.getBytes();
byteconvertStringToByte(String strValue)
Converts a given string into a byte of one integer
try {
    byte intRegVal = 0;
    for (int i = strValue.length(); i > 0; i--) {
        intRegVal = (byte) (intRegVal + ((int) Math.pow(16, strValue.length() - i))
                * Integer.parseInt(strValue.substring(i - 1, i), 16));
    return intRegVal;
} catch (NumberFormatException e) {
...
byte[]convertStringToByteArray(String input)
Helper-method to emulate the getByte() method missing from the JRE emulation library.
char[] chars = input.toCharArray();
byte[] bytes1 = new byte[chars.length * 2];
int i = 0;
for (char c : chars) {
    if (c < 128) {
        bytes1[i] = (byte) c;
        i++;
    } else {
...
byte[]convertStringToByteArray(String string)
convert String To Byte Array
if (string == null) {
    throw new IllegalArgumentException("String argument must not be null!");
if (string.length() % 2 != 0) {
    throw new IllegalArgumentException("String argument has to have a length which can be divided by 2!");
byte[] bytes = new byte[string.length() / 2];
StringBuffer buffer = new StringBuffer(string);
...
byte[]convertStringToBytes(String string)
convert String To Bytes
if (string.length() % 2 != 0) {
    throw new IllegalArgumentException("Illegal HEX string '" + string
            + "' provided! The number of character needs to be dividable by 2.");
byte[] result = new byte[string.length() / 2];
char[] chars = new char[string.length()];
string.getChars(0, string.length(), chars, 0);
for (int i = 0; i < string.length() / 2; i++) {
...
byte[]getBytes(String k)
get Bytes
if (k == null || k.length() == 0) {
    throw new IllegalArgumentException("Key must not be blank");
try {
    return k.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
byte[]getBytes(String k)
get Bytes
if (k == null || k.length() == 0) {
    throw new IllegalArgumentException("Key must not be blank");
try {
    return k.getBytes(DEFAULT_CHARSET_NAME);
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);