Java - Write code to convert byte array to to Hex String in Upper Case

Requirements

Write code to convert byte array to to Hex String in Upper Case

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(toHexStringUpperCase(b));
    }/* w w w.  j av  a 2s  . c o  m*/

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

    public static String toHexStringUpperCase(byte[] b) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            sb.append(hexCharsUpperCase[(int) (((int) b[i] >> 4) & 0x0f)]);
            sb.append(hexCharsUpperCase[(int) (((int) b[i]) & 0x0f)]);
        }
        return sb.toString();
    }
}

Related Exercise