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

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

Description

This method converts an array of bytes to a hex string.

License

Open Source License

Declaration

public static String bytesToHex(byte[] bytes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();

    /**// ww  w  . ja  v a  2  s  .c  om
     * This method converts an array of bytes to a hex string.
     */
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 3];

        for (int index = 0; index < bytes.length; index++) {
            int v = bytes[index] & 0xFF;
            hexChars[index * 3] = hexArray[v >>> 4];
            hexChars[index * 3 + 1] = hexArray[v & 0x0F];
            hexChars[index * 3 + 2] = ' ';
        }

        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, byte[] hex, int offset)
  9. bytesToHex(byte[] bytes, int groupSize)