Here you can find the source of bytesAsHexString(byte[] bytes, int maxShowBytes)
public static String bytesAsHexString(byte[] bytes, int maxShowBytes)
//package com.java2s; public class Main { public static String bytesAsHexString(byte[] bytes, int maxShowBytes) { int idx = 0; StringBuilder body = new StringBuilder(); body.append("bytes size is:["); body.append(bytes.length);/*from w w w .ja v a2s. c o m*/ body.append("]\r\n"); for (byte b : bytes) { int hex = ((int) b) & 0xff; String shex = Integer.toHexString(hex).toUpperCase(); if (1 == shex.length()) { body.append("0"); } body.append(shex); body.append(" "); idx++; if (16 == idx) { body.append("\r\n"); idx = 0; } maxShowBytes--; if (maxShowBytes <= 0) { break; } } if (idx != 0) { body.append("\r\n"); } return body.toString(); } }