Android Byte Array to Hex Convert toHexString(byte abyte0[])

Here you can find the source of toHexString(byte abyte0[])

Description

Method convert byte[] to HexString

License

Open Source License

Parameter

Parameter Description
The string to be format.

Declaration

public static String toHexString(byte abyte0[]) 

Method Source Code

//package com.java2s;

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 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]);/*ww  w  .j a v  a  2  s.  c o m*/
        }
        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;
    }

    private static void appendHex(StringBuffer stringbuffer, byte byte0) {
        stringbuffer.append(toHexChar(byte0 >> 4));
        stringbuffer.append(toHexChar(byte0));
    }

    /**
     * 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);
    }

    /**
     * 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. toHex(final byte[] bytes)
  2. byteArrayToHexString(byte[] b)
  3. toHexString(byte abyte0[], int beginIndex, int endIndex, boolean spaceFlag)
  4. toHexString(byte abyte0[], int beginIndex, int endIndex)
  5. toHexString(byte abyte0[], boolean spaceFlag)
  6. toHexString(byte[] bytes)
  7. encodeHexStr(final byte[] bytes)
  8. byteArrayToHexString(byte[] b)
  9. getHexString(byte[] raw)