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

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

Description

Converts the provided byte array into a hexadecimal string with two characters per byte.

License

Open Source License

Declaration

public static String bytesToHex(byte[] bytes) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*  ww  w .  j a v a2s . c  om*/
     * Converts the provided byte array into a hexadecimal string
     * with two characters per byte.
     */
    public static String bytesToHex(byte[] bytes) {
        return bytesToHex(bytes, false);
    }

    /**
     * Takes the provided byte array and converts it into a hexadecimal string
     * with two characters per byte.
     *
     * @param withSpaces if true, include a space character between each hex-rendered
     *                   byte for readability.
     */
    public static String bytesToHex(byte[] bytes, boolean withSpaces) {
        StringBuilder sb = new StringBuilder();
        for (byte hashByte : bytes) {
            int intVal = 0xff & hashByte;
            if (intVal < 0x10) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(intVal));
            if (withSpaces) {
                sb.append(' ');
            }
        }
        return sb.toString();
    }
}

Related

  1. bytesToHex(byte[] binary)
  2. bytesToHex(byte[] bs, int off, int length)
  3. bytesToHex(byte[] bt)
  4. bytesToHex(byte[] buf)
  5. bytesToHex(byte[] bytes)
  6. bytesToHex(byte[] bytes)
  7. bytesToHex(byte[] bytes)
  8. bytesToHex(byte[] bytes)
  9. bytesToHex(byte[] bytes)