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

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

Description

Convert an array of arbitrary bytes into a String of hexadecimal number-pairs with each pair representing on byte of the array.

License

Open Source License

Parameter

Parameter Description
bytes the array to convert into hexadecimal string

Return

the String containing the hexadecimal representation of the array

Declaration

public static String bytesToHex(byte[] bytes) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  w  ww  .j a va 2 s  .co m
     * Convert an array of arbitrary bytes into a String of hexadecimal number-pairs with each pair representing on byte
     * of the array.
     *
     * @param bytes the array to convert into hexadecimal string
     * @return the String containing the hexadecimal representation of the array
     */
    public static String bytesToHex(byte[] bytes) {
        final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] hexChars = new char[bytes.length << 1];
        for (int i = 0; i < bytes.length; i++) {
            int value = bytes[i] & 0xFF;
            int baseIndex = i << 1;
            hexChars[baseIndex] = hexArray[value >>> 4];
            hexChars[baseIndex + 1] = hexArray[value & 0x0F];
        }
        return new String(hexChars);
    }
}

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)