Java Hex Calculate toHexString(byte[] b)

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

Description

Convert bytes to hex string (all lower-case).

License

Open Source License

Parameter

Parameter Description
b Input bytes.

Return

Hex string.

Declaration

public static String toHexString(byte[] b) 

Method Source Code

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

public class Main {
    static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    /**//ww  w  . ja v a  2  s .  c o m
     * Convert bytes to hex string (all lower-case).
     * 
     * @param b
     *            Input bytes.
     * @return Hex string.
     */
    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length << 2);
        for (byte x : b) {
            int hi = (x & 0xf0) >> 4;
            int lo = x & 0x0f;
            sb.append(HEX_CHARS[hi]);
            sb.append(HEX_CHARS[lo]);
        }
        return sb.toString().trim();
    }
}

Related

  1. toHexString(byte[] b)
  2. toHexString(byte[] b)
  3. toHexString(byte[] b)
  4. toHexString(byte[] b)
  5. toHexString(byte[] b)
  6. toHexString(byte[] b, int off, int len)
  7. toHexString(byte[] b, int off, int len)
  8. toHexString(byte[] ba)
  9. toHexString(byte[] ba)