Convert byte array to Hex String by offset and space flag - Android java.lang

Android examples for java.lang:Byte Array

Description

Convert byte array to Hex String by offset and space flag

Demo Code


public class Main{

    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 a2s . co 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;
    }

}

Related Tutorials