Here you can find the source of toHexString(byte[] bytes)
Parameter | Description |
---|---|
bytes | input bytes |
public static String toHexString(byte[] bytes)
//package com.java2s; //License from project: Open Source License public class Main { private static final char[] HEXCHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /**/* w w w .j a v a 2s .co m*/ * converts all bytes into a hex string of length bytes.length * 2 * @param bytes input bytes * @return a hex representation of bytes */ public static String toHexString(byte[] bytes) { return toHexString(bytes, 0, bytes.length, 0); } /** * converts all bytes into a hex string of length bytes.length * 2 * @param bytes input bytes * @return a hex representation of bytes */ public static String toHexString(byte[] bytes, int off, int len) { return toHexString(bytes, off, len, 0); } /** * converts all bytes into a hex string of length bytes.length * 2 * @param bytes input bytes * @param space when to set spaces (byte 1) and when to add newlines (byte 2) * @return a hex representation of bytes */ public static String toHexString(byte[] bytes, int space) { return toHexString(bytes, 0, bytes.length, space); } /** * converts all bytes into a hex string of length bytes.length * 2 * @param bytes input bytes * @param space when to set spaces (byte 1) and when to add newlines (byte 2) * @return a hex representation of bytes */ public static String toHexString(byte[] bytes, int off, int len, int space) { if (bytes.length == 0) return ""; String LS = System.lineSeparator(); int s = space & 0xFF, nl = space >> 8 & 0xFF; int pos = 0, clen = bytes.length * 2; if (s > 0) clen += (bytes.length - 1) / s; if (nl > 0) clen += (bytes.length - 1) / nl * LS.length(); char[] chars = new char[clen]; for (int i = 0; i < len; i++) { chars[pos++] = HEXCHARS[bytes[off + i] >> 4 & 0xF]; chars[pos++] = HEXCHARS[bytes[off + i] & 0xF]; if (i + 1 < len) { if (s > 0 && i % s == s - 1) { if (nl > 0 && i % nl == nl - 1) { LS.getChars(0, LS.length(), chars, pos); pos += LS.length(); } else chars[pos++] = ' '; } else if (nl > 0 && i % nl == nl - 1) { LS.getChars(0, LS.length(), chars, pos); pos += LS.length(); } } } return new String(chars, 0, pos); } }