Here you can find the source of toHexString(final byte[] b)
Parameter | Description |
---|---|
b | the byte array to convert |
public static String toHexString(final byte[] b)
//package com.java2s; //License from project: Apache License public class Main { /**/* w w w . ja v a 2s . co m*/ * The hex digits used to build a hex string representation of a byte array. */ public static final char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * Converts a byte array to a hex string representing it. The hex digits are * lower case. * * @param b the byte array to convert * @return the hex representation of b */ public static String toHexString(final byte[] b) { final StringBuilder sb = new StringBuilder(); for (final byte element : b) { sb.append(hexChar[(element & 0xf0) >>> 4]); sb.append(hexChar[element & 0x0f]); } return sb.toString(); } /** * Extends or truncate string to length if necessary and add to the string * builder. * * @param sb * @param str * @param length */ public static void append(final StringBuilder sb, final String str, final int length) { assert sb != null; if (str == null) { for (int i = 0; i < length; i++) { sb.append(' '); } } else { final int sl = str.length(); if (sl < length) { sb.append(str); for (int i = sl; i < length; i++) { sb.append(' '); } } else { sb.append(str.subSequence(0, length)); } } } }