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[]fromString(String binary)
Does the oposite of the function \c toString(byte[]).
char[] nibbles;
int i, x, limit = strings.length(binary);
if (limit == 0)
    return new byte[0];
nibbles = new char[limit];
for (i = 0, x = 0; i < limit; i++) {
    if (strings.isHexChar(binary.charAt(i)))
        nibbles[x++] = (char) Character.digit(binary.charAt(i), 16);
...
byte[]fromString(String str)
Convert a hex-encoded String to binary data
ByteArrayOutputStream bs = new ByteArrayOutputStream();
byte[] raw = str.getBytes();
for (int i = 0; i < raw.length; i++) {
    if (!Character.isWhitespace((char) raw[i]))
        bs.write(raw[i]);
byte[] in = bs.toByteArray();
if (in.length % 2 != 0) {
...
byte[]hexStringToByteArray(String hexa)
hex String To Byte Array
if (hexa.length() % 2 != 0) {
    throw new IllegalArgumentException("String hexa inv?lida");
byte[] b = new byte[hexa.length() / 2];
for (int i = 0; i < hexa.length(); i += 2) {
    b[i / 2] = (byte) ((hexDigits.indexOf(hexa.charAt(i)) << 4) | (hexDigits
            .indexOf(hexa.charAt(i + 1))));
return b;
byte[]toByte(String hexString)
to Byte
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
    result[i] = Integer.valueOf(
            hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
return result;
byte[]toByte(String hexString)
to Byte
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
    result[i] = Integer.valueOf(
            hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
return result;
byte[]toBytes(final String str)
to Bytes
if (str == null) {
    return null;
try {
    return str.getBytes(CHARSET_NAME_UTF8);
} catch (final UnsupportedEncodingException e) {
    throw new RuntimeException(e.getMessage(), e);
byte[]StringtoByte(String data)
Stringto Byte
try {
    return data.getBytes(DEFAULT_CHARSET);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
return null;
intgetLengthByByte(String str)
get Length By Byte
int length = 0;
if (str == null || str.length() == 0) {
    return length;
for (int i = 0; i < str.length(); i++) {
    int ascii = Character.codePointAt(str, i);
    if (ascii >= 0 && ascii <= 255) {
        length++;
...
intbytesStorage(String str)
bytes Storage
String s = new String(str);
int len = 0;
for (int i = 0; i < s.length(); i = s.offsetByCodePoints(i, 1)) {
    len += bytesUtf8(s.codePointAt(i));
return len;
intbytesStorage(String str)
bytes Storage
String s = new String(str);
int len = 0;
for (int i = 0; i < s.length(); i = s.offsetByCodePoints(i, 1)) {
    len += bytesUtf8(s.codePointAt(i));
return len;