Android Byte Array to Hex Convert toHexString(byte abyte0[], int beginIndex, int endIndex)

Here you can find the source of toHexString(byte abyte0[], int beginIndex, int endIndex)

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(byte abyte0[], int beginIndex,
            int endIndex) 

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]);/* w w w  .j  a v a  2  s.c  om*/
        }
        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. bytesToHexString(byte[] bytes)
  2. convertToHex(byte[] data)
  3. toHex(final byte[] bytes)
  4. byteArrayToHexString(byte[] b)
  5. toHexString(byte abyte0[], int beginIndex, int endIndex, boolean spaceFlag)
  6. toHexString(byte abyte0[], boolean spaceFlag)
  7. toHexString(byte abyte0[])
  8. toHexString(byte[] bytes)
  9. encodeHexStr(final byte[] bytes)