Android Array to String Convert byteToString(byte[] str)

Here you can find the source of byteToString(byte[] str)

Description

byte To String

License

Open Source License

Declaration

public static String byteToString(byte[] str) 

Method Source Code


public class Main{
    /** A table of hex digits */
    public static final char[] hexDigit = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    public static String byteToString(byte[] str) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < str.length; i++) {
            sb.append(StringUtil.toHexString(str[i]));
            sb.append(" ");
        }/*from   ww w .  j ava2  s  .  c  om*/

        return sb.toString();
    }
    public static String toHexString(byte abyte0[], int beginIndex,
            int endIndex, boolean spaceFlag) {
        if (null == abyte0)
            return null;
        if (0 == abyte0.length)
            return "";
        StringBuffer sbuf = new StringBuffer();
        appendHex(sbuf, abyte0[beginIndex]);
        for (int i = (beginIndex + 1); i < endIndex; i++) {
            if (spaceFlag)
                sbuf.append(' ');
            appendHex(sbuf, abyte0[i]);
        }
        String returnString = sbuf.toString();
        sbuf = null;
        return returnString;
    }
    public static String toHexString(byte abyte0[], int beginIndex,
            int endIndex) {
        if (null == abyte0)
            return null;
        return toHexString(abyte0, beginIndex, endIndex, true);
    }
    public static String toHexString(byte abyte0[], boolean spaceFlag) {
        if (null == abyte0)
            return null;
        return toHexString(abyte0, 0, abyte0.length, spaceFlag);
    }
    /**
     * Method convert byte[] to HexString
     *
     * @param The string to be format.
     *
     */
    public static String toHexString(byte abyte0[]) {
        if (null == abyte0)
            return null;
        return toHexString(abyte0, 0, abyte0.length, true);
    }
    public static String toHexString(char achar0) {
        return toHexString((byte) achar0);
    }
    public static String toHexString(byte abyte0) {
        StringBuffer sbuf = new StringBuffer();
        appendHex(sbuf, abyte0);

        String returnString = sbuf.toString();
        sbuf = null;
        return returnString;
    }
    /**
     * Method convert byte[] to String
     *
     * @param The string to be format.
     *
     */
    public static String toString(byte[] buffer) {
        if (null == buffer)
            return null;
        else
            return new String(buffer);
    }
    private static void appendHex(StringBuffer stringbuffer, byte byte0) {
        stringbuffer.append(toHexChar(byte0 >> 4));
        stringbuffer.append(toHexChar(byte0));
    }
    /**
     * Convert a nibble to a hex character
     * 
     * @param nibble
     *          the nibble to convert.
     */
    public static char toHexChar(int nibble) {
        return hexDigit[(nibble & 0xF)];
    }
}

Related

  1. byteArrayToString(byte[] a)
  2. bytetoString(byte[] b)
  3. hexToString(byte[] ids)
  4. toString(byte[] ba)
  5. toString(byte[] ba, int offset, int length)
  6. toString(byte[] buffer)
  7. toString(final byte[] bytes)
  8. toString(final byte[] bytes, String charset)
  9. toStringArrayToString(String[] array)