Java - Write code to convert byte hex

Requirements

Write code to convert byte hex

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        byte[] b = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(byte2hex(b));
    }//from   ww w. ja  v  a2 s  . c  o  m

    final private static char[] hexChars = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    /**
     * 
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) {
        return byte2hex(b, (char) 0);
    }

    private static void byte2hex(byte b, StringBuffer buf) {
        int high = ((b & 0xf0) >> 4);
        int low = (b & 0x0f);
        buf.append(hexChars[high]);
        buf.append(hexChars[low]);
    }

    public static String byte2hex(byte b) {
        int high = b;
        high = (high & 0xf0) >> 4; // System.out.print(Integer.toHexString(high));
        int low = (b & 0x0f); // System.out.print(Integer.toHexString(low));
        return "" + hexChars[high] + hexChars[low];
    }

    /**
     * 
     * @param b
     * @param delimiter
     * @return
     */
    public static String byte2hex(byte[] b, char delimiter) {
        StringBuffer sb = new StringBuffer();
        for (int n = 0; n < b.length; n++) {
            byte2hex(b[n], sb);
            if (n < (b.length - 1) && delimiter != 0)
                sb.append(delimiter);
        }
        return sb.toString().toLowerCase();
    }
}