Here you can find the source of bytesToHex(byte[] bytes)
byte
array.
Parameter | Description |
---|---|
bytes | the <code>byte</code> array to be expressed |
public static String bytesToHex(byte[] bytes)
//package com.java2s; public class Main { /**//from w ww. ja v a2 s . c o m * Creates a lowercase hexadecimal {@link String} representing a * <code>byte</code> array. * * @param bytes the <code>byte</code> array to be expressed * @return the resulting {@link String}, in which each input byte has been * turned into two hexadecimal characters. */ public static String bytesToHex(byte[] bytes) { char[] buffer = new char[2 * (bytes.length)]; int cursor = 0; for (byte b : bytes) { int nibble1 = 0x0F & b >> 4; int nibble2 = 0x0F & b; buffer[cursor++] = (char) (nibble1 < 10 ? nibble1 + '0' : nibble1 - 10 + 'a'); buffer[cursor++] = (char) (nibble2 < 10 ? nibble2 + '0' : nibble2 - 10 + 'a'); } return new String(buffer); } }