Android Byte Array to String Convert toString(byte[] array)

Here you can find the source of toString(byte[] array)

Description

Converts a byte array into its string representation.

Parameter

Parameter Description
array Byte array with values to convert.

Declaration

public static String toString(byte[] array) 

Method Source Code

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

public class Main{
    /**/*from  w w w .j a va2s  .  c  o m*/
     * Covert each byte of the array in a two characters string
     * representation.
     * @param array Byte array with values to convert.
     * @param separator A string to separate groups. Can be \b null.
     * @param frequency The frequency to separate groups. See the example.
     * When \a separtor is \b null, \a frequency is ignored.
     * @returns A string with the converted value.
     * @remarks The conversion is done byte by byte, building its
     * representation in a string notation. For example:
     * <pre>
     * byte[] array = { 0x01, 0xA0, 0x2F, 0xFF };
     * arrays.toString(array, "-", 2);
     * arrays.toString(array, " ", 4);
     * </pre>
     * Then it will be converted in this string:
     * <pre>
     * "01-A0-2F-FF"
     * "01A0 2FFF"
     * </pre>
     * Where each byte is represented by two characters.
     **/
    public static String toString(byte[] array, String separator,
            int frequency) {
        int limit = arrays.length(array);
        int extra = (separator == null ? 0 : separator.length() * limit);
        StringBuilder sb = new StringBuilder(limit * 2 + extra);

        if (separator != null) {
            for (int i = 0; i < limit; i++) {
                if ((i > 0) && (((i + 1) % frequency) == 0))
                    sb.append(separator);
                sb.append(String.format("%02X", array[i]));
            }
        } else {
            for (int i = 0; i < limit; i++)
                sb.append(String.format("%02X", array[i]));
        }
        return sb.toString();
    }
    /**
     * Converts a byte array into its string representation.
     * This version is similar to the default \c toString(byte[],String,int),
     * as of passed the \a separator parameter as \b null.
     * @param array Byte array with values to convert.
     * @returns A string representation of the array passed in.
     **/
    public static String toString(byte[] array) {
        return toString(array, null, 0);
    }
    /**
     * 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);
    }
    /**
     * \copydoc append(Object[],Object[])
     **/
    public static byte[] append(byte[] arr1, byte[] arr2) {
        if (arr1 == null)
            return arr2;
        if (arr2 == null)
            return arr1;

        int size = arrays.length(arr1);
        arr1 = arrays.realloc(arr1, size + arrays.length(arr2));
        arrays.copy(arr1, size, arr2, 0, arr2.length);
        return arr1;
    }
    /**
     * \copydoc append(Object[],Object[])
     **/
    public static char[] append(char[] arr1, char[] arr2) {
        if (arr1 == null)
            return arr2;
        if (arr2 == null)
            return arr1;

        int size = arrays.length(arr1);
        arr1 = arrays.realloc(arr1, size + arrays.length(arr2));
        arrays.copy(arr1, size, arr2, 0, arr2.length);
        return arr1;
    }
    /**
     * \copydoc append(Object[],Object[])
     **/
    public static short[] append(short[] arr1, short[] arr2) {
        if (arr1 == null)
            return arr2;
        if (arr2 == null)
            return arr1;

        int size = arrays.length(arr1);
        arr1 = arrays.realloc(arr1, size + arrays.length(arr2));
        arrays.copy(arr1, size, arr2, 0, arr2.length);
        return arr1;
    }
    /**
     * \copydoc append(Object[],Object[])
     **/
    public static int[] append(int[] arr1, int[] arr2) {
        if (arr1 == null)
            return arr2;
        if (arr2 == null)
            return arr1;

        int size = arrays.length(arr1);
        arr1 = arrays.realloc(arr1, size + arrays.length(arr2));
        arrays.copy(arr1, size, arr2, 0, arr2.length);
        return arr1;
    }
    /**
     * \copydoc append(Object[],Object[])
     **/
    public static long[] append(long[] arr1, long[] arr2) {
        if (arr1 == null)
            return arr2;
        if (arr2 == null)
            return arr1;

        int size = arrays.length(arr1);
        arr1 = arrays.realloc(arr1, size + arrays.length(arr2));
        arrays.copy(arr1, size, arr2, 0, arr2.length);
        return arr1;
    }
    /**
     * \copydoc append(Object[],Object[])
     **/
    public static String[] append(String[] arr1, String[] arr2) {
        if (arr1 == null)
            return arr2;
        if (arr2 == null)
            return arr1;

        int size = arrays.length(arr1);
        arr1 = arrays.realloc(arr1, size + arrays.length(arr2));
        arrays.copy(arr1, size, arr2, 0, arr2.length);
        return arr1;
    }
    /**
     * Append one array to another.
     * \param arr1 The main array. The second array will be appended to this
     *      one.
     * \param arr2 The second array, to be appended in the first one.
     * \return An array resulting of the operation.
     * \remarks If \a arr1 is \b null, \a arr2 will be returned. If \a arr2 is
     * \b null, \a arr1 will be returned without any changes.
     **/
    public static Object[] append(Object[] arr1, Object[] arr2) {
        if (arr1 == null)
            return arr2;
        if (arr2 == null)
            return arr1;

        int size = arrays.length(arr1);
        arr1 = arrays.realloc(arr1, size + arrays.length(arr2));
        arrays.copy(arr1, size, arr2, 0, arr2.length);
        return arr1;
    }
    /**
     * Reallocate an array.
     * The function creates a new array of size \a size, copies the data from
     * the current \a array to the new created array and returns it.
     * \param array The array with current values to fill the new array.
     * \param size The length of the new array.
     * \return If succeeds the function returns the newly created array. If
     * fails the function returns \a array.
     **/
    public static byte[] realloc(byte[] array, int size) {
        int count = arrays.length(array);
        byte[] memory = new byte[size];
        if (memory == null)
            return array;
        if (array == null)
            return memory;
        arrays.copy(memory, 0, array, 0, ((size < count) ? size : count));
        return memory;
    }
    /**
     * \copydoc realloc(byte[],int)
     **/
    public static char[] realloc(char[] array, int size) {
        int count = arrays.length(array);
        char[] memory = new char[size];
        if (memory == null)
            return array;
        if (array == null)
            return memory;
        arrays.copy(memory, 0, array, 0, ((size < count) ? size : count));
        return memory;
    }
    /**
     * \copydoc realloc(byte[],int)
     **/
    public static short[] realloc(short[] array, int size) {
        int count = arrays.length(array);
        short[] memory = new short[size];
        if (memory == null)
            return array;
        if (array == null)
            return memory;
        arrays.copy(memory, 0, array, 0, ((size < count) ? size : count));
        return memory;
    }
    /**
     * \copydoc realloc(byte[],int)
     **/
    public static int[] realloc(int[] array, int size) {
        int count = arrays.length(array);
        int[] memory = new int[size];
        if (memory == null)
            return array;
        if (array == null)
            return memory;
        arrays.copy(memory, 0, array, 0, ((size < count) ? size : count));
        return memory;
    }
    /**
     * \copydoc realloc(byte[],int)
     **/
    public static long[] realloc(long[] array, int size) {
        int count = arrays.length(array);
        long[] memory = new long[size];
        if (memory == null)
            return array;
        if (array == null)
            return memory;
        arrays.copy(memory, 0, array, 0, ((size < count) ? size : count));
        return memory;
    }
    /**
     * \copydoc realloc(byte[],int)
     **/
    public static String[] realloc(String[] array, int size) {
        int count = arrays.length(array);
        String[] memory = new String[size];
        if (memory == null)
            return array;
        if (array == null)
            return memory;
        arrays.copy(memory, 0, array, 0, ((size < count) ? size : count));
        return memory;
    }
    /**
     * \copydoc realloc(byte[],int)
     **/
    public static Object[] realloc(Object[] array, int size) {
        int count = arrays.length(array);
        Object[] memory = new Object[size];
        if (memory == null)
            return array;
        if (array == null)
            return memory;
        arrays.copy(memory, 0, array, 0, ((size < count) ? size : count));
        return memory;
    }
    /**
     * Copy elements from one array to another.
     * \param dst The destination array.
     * \param dstInd The element, in the destination array, where the copy
     *      should start.
     * \param src The source array.
     * \param srcInd The element, int the source array, where the copy should
     *      start.
     * \param count The number of elements to copy from \a src to \a dst.
     * \remarks \a srcInd and \a dstInd must be valid. No check is done in
     * these arguments. \a count can be less than zero if the entire \a src
     * array should be copied to \a dst. In cases where the number of elements
     * to copy are zero, no objects are touched by this function.
     * \note Arguments are in different order of \c System.arraycopy()
     * standard function.
     **/
    public static void copy(byte[] dst, int dstInd, byte[] src, int srcInd,
            int count) {
        if (count < 0) {
            count = (arrays.length(src) - srcInd);
        }
        for (int i = 0; i < count; i++) {
            dst[i + dstInd] = src[i + srcInd];
        }
    }
    /**
     * \copydoc copy(byte[],int,byte[],int,int);
     **/
    public static void copy(char[] dst, int dstInd, char[] src, int srcInd,
            int count) {
        if (count < 0) {
            count = arrays.length(src) - srcInd;
        }
        for (int i = 0; i < count; i++) {
            dst[i + dstInd] = src[i + srcInd];
        }
    }
    /**
     * \copydoc copy(byte[],int,byte[],int,int);
     **/
    public static void copy(short[] dst, int dstInd, short[] src,
            int srcInd, int count) {
        if (count < 0) {
            count = arrays.length(src) - srcInd;
        }
        for (int i = 0; i < count; i++) {
            dst[i + dstInd] = src[i + srcInd];
        }
    }
    /**
     * \copydoc copy(byte[],int,byte[],int,int);
     **/
    public static void copy(int[] dst, int dstInd, int[] src, int srcInd,
            int count) {
        if (count < 0) {
            count = arrays.length(src) - srcInd;
        }
        for (int i = 0; i < count; i++) {
            dst[i + dstInd] = src[i + srcInd];
        }
    }
    /**
     * \copydoc copy(byte[],int,byte[],int,int);
     **/
    public static void copy(long[] dst, int dstInd, long[] src, int srcInd,
            int count) {
        if (count < 0) {
            count = arrays.length(src) - srcInd;
        }
        for (int i = 0; i < count; i++) {
            dst[i + dstInd] = src[i + srcInd];
        }
    }
    /**
     * \copydoc copy(byte[],int,byte[],int,int);
     **/
    public static void copy(String[] dst, int dstInd, String[] src,
            int srcInd, int count) {
        if (count < 0) {
            count = arrays.length(src) - srcInd;
        }
        for (int i = 0; i < count; i++) {
            dst[i + dstInd] = src[i + srcInd];
        }
    }
    /**
     * \copydoc copy(byte[],int,byte[],int,int)
     **/
    public static void copy(Object[] dst, int dstInd, Object[] src,
            int srcInd, int count) {
        if (count < 0) {
            count = arrays.length(src) - srcInd;
        }
        for (int i = 0; i < count; i++) {
            dst[i + dstInd] = src[i + srcInd];
        }
    }
}

Related

  1. toAsciiString(byte[] raw)
  2. toString(byte[] array, String separator, int frequency)
  3. toString(byte[] b)
  4. toString(byte[] theByteArray)
  5. fromBytes(byte[] buf)