Java - Write code to Convert byte array to Hex String using shift operator

Requirements

Write code to to Hex String

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        byte[] bs = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(toHexString(bs));
    }/*  ww  w .  j  av a2  s.co m*/

    private static final char[] hexArray = "0123456789abcdef".toCharArray();

    public static final String toHexString(byte[] bs) {
        if (bs == null)
            return null;
        char[] hexChars = new char[bs.length * 2];
        for (int i = 0; i < bs.length; i++) {
            int v = bs[i] & 0xff;
            hexChars[i * 2] = hexArray[v >>> 4];
            hexChars[i * 2 + 1] = hexArray[v & 0x0f];
        }
        return new String(hexChars);
    }
}