Here you can find the source of fromString(String binary)
Parameter | Description |
---|---|
binary | The byte array representation in a string. |
public static byte[] fromString(String binary)
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); } }