Java Hex Print printHex(byte[] bytes)

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

Description

Print out a byte array in hexadecimal

License

Open Source License

Parameter

Parameter Description
bytes array

Declaration

public static void printHex(byte[] bytes) 

Method Source Code

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

public class Main {
    /**/*from  www  . j a v  a  2s.com*/
     * Print out a byte array in hexadecimal
     *
     * @param bytes array
     */
    public static void printHex(byte[] bytes) {
        for (byte b : bytes)
            System.out.print(byteToHexString(b));
    }

    /**
     * convert a byte to a hex string
     *
     * @param data array
     * @return String representation in hex
     */
    public static String byteToHexString(byte data) {

        StringBuffer buf = new StringBuffer();
        buf.append(toHexChar((data >>> 4) & 0x0F));
        buf.append(toHexChar(data & 0x0F));

        return buf.toString();
    }

    /**
     * convert a integer into a hexadecimal character
     *
     * @param i integer
     * @return hex char
     */
    public static char toHexChar(int i) {
        if ((0 <= i) && (i <= 9)) {
            return (char) ('0' + i);
        } else {
            return (char) ('a' + (i - 10));
        }
    }
}

Related

  1. printHex(byte b)
  2. printHex(byte[] array)
  3. printHex(byte[] array, int offset, int len)
  4. printHex(byte[] b)
  5. printHex(byte[] bytes)
  6. printHex(byte[] data)
  7. printHex(byte[] field, int start, int len)
  8. printHexa(final byte b)
  9. printHexBytes(byte[] _theBytes)