Android String to Byte Array Convert fromString(String binary)

Here you can find the source of fromString(String binary)

Description

Does the oposite of the function \c toString(byte[]).

Parameter

Parameter Description
binary The byte array representation in a string.

Return

The byte array extracted from the string.

Declaration

public static byte[] fromString(String binary) 

Method Source Code

import java.util.Random;
import x.android.defs.ERROR;

public class Main{
    /**//w  ww .  j  a  v a  2s. c o  m
     * Does the oposite of the function \c toString(byte[]).
     * Converts a byte array representation in a string into a real byte
     * array. This function supports separators, as of the \c toString()
     * version, that can return string using separators. Any character is
     * accepted as a separator along it is not a valid hexadecimal character.
     * @param binary The byte array representation in a string.
     * @return The byte array extracted from the string.
     **/
    public static byte[] fromString(String binary) {
        char[] nibbles;
        int i, x, limit = strings.length(binary);

        if (limit == 0)
            return new byte[0];

        /* First we remove any non hexadecimal character, forming an array of
         * remaining values.
         */
        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);
        }

        /* Now we converte each two characters into a byte value. */
        int value;
        byte[] result = new byte[x / 2]; /* x has the final number of nibbles found. */

        limit = result.length * 2; /* In the case when x is odd. */
        i = x = 0;
        while (i < limit) {
            value = nibbles[i++] * 16;
            value += nibbles[i++];
            result[x++] = (byte) (value & 0x000000FF);
        }
        return result;
    }
    /**
     * Safely gets the length of an array.
     * \param array The array thats the length should be retrieved.
     * \returns The length of the array. If \a array is \b null the return will
     * be zero.
     **/
    public static int length(byte[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(char[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(short[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(int[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(long[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(String[] array) {
        return ((array == null) ? 0 : array.length);
    }
    /**
     * \copydoc arrays#length(byte[])
     **/
    public static int length(Object[] array) {
        return ((array == null) ? 0 : array.length);
    }
}

Related

  1. fromString(String str)
  2. hexStringToByteArray(String hexa)
  3. toByte(String hexString)
  4. toByte(String hexString)