Java - Write code to convert byte array To Hex String with for loop and String.format()

Requirements

Write code to byte To Hex String

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        byte[] byteArray = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(byteToHexString(byteArray));
    }/*from   w  w w .  java2s.c om*/

    public static String byteToHexString(byte[] byteArray) {
        StringBuilder sb = new StringBuilder(byteArray.length * 3);
        for (byte b : byteArray) {
            sb.append(byteToHex(b));
        }
        return sb.toString();
    }

    public static String byteToHex(byte b) {
        return String.format("x%02X", b);
    }
}

Related Example