Java Byte to Hex bytesToHex(byte b)

Here you can find the source of bytesToHex(byte b)

Description

bytes To Hex

License

Open Source License

Declaration

public static String bytesToHex(byte b) 

Method Source Code

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

public class Main {
    /** Most significant bits mask for binary operations */
    public static final int MOST_SIGNIFICANT_MASK = 0xFF;
    /** Least significant bits mask for binary operations */
    public static final int LEAST_SIGNIFICANT_MASK = 0x0F;
    /** {@code char[]} in which each position contains the hex value textual representation */
    protected static final char[] hexArray = "0123456789ABCDEF"
            .toCharArray();

    public static String bytesToHex(byte b) {
        return bytesToHex(new byte[] { b });
    }/*from ww  w  .  ja va2 s .  com*/

    /**
     * Converts each byte nibble to the equivalent hex representation, in a char
     * @param bytes The bytes
     * @return A {@link String} containing the hex representation of each nibble
     */
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & MOST_SIGNIFICANT_MASK;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & LEAST_SIGNIFICANT_MASK];
        }
        return new String(hexChars);
    }
}

Related

  1. bytes2hex(byte[] bts)
  2. bytes2hex(byte[] bytes)
  3. bytes2hex(byte[] bytes)
  4. bytes2hex(byte[] bytes)
  5. Bytes2HexString(byte[] b)
  6. bytesToHexs(byte[] buf)
  7. bytesToLowerCaseHex(byte[] data)
  8. bytesToModHex(final byte[] inputBytes)
  9. bytesToPrettyHex(byte[] data)