Here you can find the source of toHex(final byte[] data, final String separator)
Parameter | Description |
---|---|
data | Data to encode. |
separator | How to separate the bytes in the output |
public static String toHex(final byte[] data, final String separator)
//package com.java2s; //License from project: Apache License public class Main { private static final char[] HEX_DIGIT = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; private static final int MAX_HEX_DIGIT = 0xF; public static char toHex(final int nibble) { return HEX_DIGIT[(nibble & MAX_HEX_DIGIT)]; }/* w w w.jav a 2 s . c om*/ /** * @param data Data to encode. * @param separator How to separate the bytes in the output * @return The byte array encoded as a string of hex digits separated by given separator. */ public static String toHex(final byte[] data, final String separator) { if (data.length == 0) { return ""; } final StringBuilder sb = new StringBuilder(data.length * (separator.length() + 2) - separator.length()); for (int i = 0; i < data.length; i++) { if (i > 0) { sb.append(separator); } sb.append(toHex((data[i] & 0xF0) >> 4)); sb.append(toHex(data[i] & 0x0F)); } return sb.toString(); } }