Java Byte Array to Hex String bytesToHexString(byte[] b)

Here you can find the source of bytesToHexString(byte[] b)

Description

Convert a vector of bytes into a regularily formatted string of hexadecimal values.

License

Open Source License

Parameter

Parameter Description
b the vector to convert

Return

the result of the processing

Declaration

public static String bytesToHexString(byte[] b) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  ww  w .  j  av a 2s . com
     * Convert a vector of bytes into a regularily formatted string
     * of hexadecimal values.
     * @param b the vector to convert
     * @return the result of the processing
     */
    public static String bytesToHexString(byte[] b) {
        String space = " ";
        StringBuilder sb = new StringBuilder();

        for (int index = 0; index < b.length; index++) {
            int tmp = b[index];
            tmp &= 0xFF;
            sb.append("0x");
            if (tmp < 16)
                sb.append(space);
            sb.append(Integer.toHexString(tmp));
            if (index != b.length - 1)
                sb.append(space);
        }

        return sb.toString();
    }
}

Related

  1. bytesToHexString(byte abyte0[], int i, int j)
  2. bytesToHexString(byte bytes[])
  3. bytesToHexString(byte data[])
  4. bytesToHexString(byte[] _bytes)
  5. bytesToHexString(byte[] array)
  6. bytesToHexString(byte[] b, int length)
  7. bytesToHexString(byte[] bArray)
  8. bytesToHexString(byte[] bArray)
  9. bytesToHexString(byte[] bs)