Java Hex Calculate toHex(byte[] data)

Here you can find the source of toHex(byte[] data)

Description

Convert data to a string of hex-digits.

License

Open Source License

Parameter

Parameter Description
data a parameter

Declaration

public static String toHex(byte[] data) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  w  w .  j a v  a 2  s  . c  om
     * Convert data to a string of hex-digits.
     *
     * @param data
     * @return
     */
    public static String toHex(byte[] data) {
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(nibbleToHex(b >>> 4));
            sb.append(nibbleToHex(b));
        }
        return sb.toString();
    }

    /**
     * Get the hex-digit representation of a value. Only the first 4 bits of
     * the value is considered.
     *
     * @param value
     * @return
     */
    public static char nibbleToHex(int value) {
        value &= 0xf;
        return (char) (value > 9 ? 'a' + (value - 10) : '0' + value);
    }
}

Related

  1. toHex(byte[] bytes)
  2. toHex(byte[] bytes, int maxlen)
  3. toHex(byte[] data)
  4. toHex(byte[] data)
  5. toHex(byte[] data)
  6. toHex(byte[] data)
  7. toHex(byte[] data)
  8. toHex(byte[] data)
  9. toHex(byte[] data, int bytesPerGroup)