Java Byte Array to Hex bytesToHex(byte[] bytes)

Here you can find the source of bytesToHex(byte[] bytes)

Description

Creates a lowercase hexadecimal String representing a byte array.

License

Open Source License

Parameter

Parameter Description
bytes the <code>byte</code> array to be expressed

Return

the resulting , in which each input byte has been turned into two hexadecimal characters.

Declaration

public static String bytesToHex(byte[] bytes) 

Method Source Code

//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);
    }
}

Related

  1. bytesToHex(byte[] bytes)
  2. bytesToHex(byte[] bytes)
  3. bytesToHex(byte[] bytes)
  4. bytesToHex(byte[] bytes)
  5. bytesToHex(byte[] bytes)
  6. bytesToHex(byte[] bytes)
  7. bytesToHex(byte[] bytes)
  8. bytesToHex(byte[] bytes)
  9. bytesToHex(byte[] bytes, byte[] hex, int offset)