Java Hex Calculate toHexString(final byte[] data)

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

Description

Converts the specified data to hex string.

License

Apache License

Parameter

Parameter Description
data data to be converted

Return

the specified data converted to hex string

Declaration

public static String toHexString(final byte[] data) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /** Digits used in the hexadecimal representation. */
    public static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    /**// w w  w .  j  a  va2 s  .  co m
     * Converts the specified data to hex string.
     * 
     * @param data data to be converted
     * 
     * @return the specified data converted to hex string
     * 
     * @see #toHexString(byte[], int, int)
     */
    public static String toHexString(final byte[] data) {
        return toHexString(data, 0, data.length);
    }

    /**
     * Converts the specified data to hex string.
     * 
     * @param data data to be converted
     * @param offset offset of the first byte to be convert
     * @param length number of bytes to be converted
     * 
     * @return the specified data converted to hex string
     * 
     * @see #toHexString(byte[])
     */
    public static String toHexString(final byte[] data, int offset, int length) {
        final StringBuilder hexBuilder = new StringBuilder(length * 2);

        for (length += offset; offset < length; offset++) {
            final byte b = data[offset];
            hexBuilder.append(HEX_DIGITS[(b & 0xff) >> 4]).append(HEX_DIGITS[b & 0x0f]);
        }

        return hexBuilder.toString();
    }
}

Related

  1. toHexString(final byte[] bytes)
  2. toHexString(final byte[] bytes)
  3. toHexString(final byte[] bytes)
  4. toHexString(final byte[] data)
  5. toHexString(final byte[] data)
  6. toHexString(final byte[] fieldData)
  7. toHexString(final byte[] raw)
  8. toHexString(final int i)
  9. toHexString(final int value)