Android Utililty Methods String to Byte Array Convert

List of utility methods to do String to Byte Array Convert

Description

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

Method

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 data)
get Bytes
return getBytes(data, "GBK");
byte[]getBytes(String data, String charsetName)
get Bytes
Charset charset = Charset.forName(charsetName);
return data.getBytes(charset);
byte[]getBytesFromAddress(String address)
get Bytes From Address
int i, j = 0;
byte[] output = new byte[BD_ADDR_LEN];
if (address != null) {
    for (i = 0; i < address.length(); i++) {
        if (address.charAt(i) != ':') {
            output[j] = (byte) Integer.parseInt(
                    address.substring(i, i + 2), 16);
            j++;
...
byte[]stringToBytes(String string)
string To Bytes
StringTokenizer tokenizer = new StringTokenizer(string, ",");
byte[] bytes = new byte[tokenizer.countTokens()];
for (int k = 0; k < bytes.length; k++)
    bytes[k] = (byte) Integer.parseInt(tokenizer.nextToken());
return bytes;
byte[]String2Byte(String hexString)
String Byte
if (hexString.length() % 2 == 1)
    return null;
byte[] ret = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2) {
    ret[i / 2] = Integer.decode(
            "0x" + hexString.substring(i, i + 2)).byteValue();
return ret;
...
byte[]StringToByteArray(String input)
String To Byte Array
if (input == null || input.equals(""))
    return new byte[] {};
else
    return input.getBytes(Charset.forName("utf-8"));
byte[]StrToBcdBytes(String s)
Str To Bcd Bytes
if (s.length() % 2 != 0) {
    s = "0" + s;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i += 2) {
    int high = c[i] - 48;
    int low = c[i + 1] - 48;
...
byte[]Str2Bcd(String asc)
Str Bcd
int len = asc.length();
int mod = len % 2;
if (mod != 0) {
    asc = "0" + asc;
    len = asc.length();
byte abt[] = new byte[len];
if (len >= 2) {
...
byte[]toBytes(String digits, int radix)
to Bytes
if (digits == null) {
    return null;
if (radix != 16 && radix != 10 && radix != 8) {
    throw new IllegalArgumentException("For input radix: \""
            + radix + "\"");
int divLen = (radix == 16) ? 2 : 3;
...