Java Hex Calculate toHexString(final byte[] buffer)

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

Description

Converts an array of bytes into an hex string.

License

Open Source License

Parameter

Parameter Description
buffer the byte buffer

Return

a string with pairs of hex digits corresponding to the byte buffer

Declaration

public static String toHexString(final byte[] buffer) 

Method Source Code

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

public class Main {
    /**//from ww  w  .j a v a  2 s  . c  om
     * Number of bits on a nibble.
     */
    public static final int BITS_SIZE_OF_NIBBLE = 4;
    /**
     * The mask for the low nibble of a byte.
     */
    public static final int INT_BYTE_LOW_NIBBLE_MASK = 0x0000000F;
    /**
     * Table of Hex digits.
     */
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    /**
     * Converts an array of bytes into an hex string.
     * 
     * @param buffer the byte buffer
     * @return a string with pairs of hex digits corresponding to the byte buffer
     */
    public static String toHexString(final byte[] buffer) {
        final char[] charBuffer = new char[buffer.length * 2];

        for (int i = 0; i < buffer.length; ++i) {
            charBuffer[i * 2] = HEX_DIGITS[(buffer[i] >> BITS_SIZE_OF_NIBBLE) & INT_BYTE_LOW_NIBBLE_MASK];
            charBuffer[i * 2 + 1] = HEX_DIGITS[buffer[i] & INT_BYTE_LOW_NIBBLE_MASK];
        }

        return new String(charBuffer);
    }
}

Related

  1. toHexString(final byte[] arr)
  2. toHexString(final byte[] array)
  3. toHexString(final byte[] b)
  4. toHexString(final byte[] bs)
  5. toHexString(final byte[] bs, final char[] myDigits)
  6. toHexString(final byte[] bytes)
  7. toHexString(final byte[] bytes)
  8. toHexString(final byte[] bytes)
  9. toHexString(final byte[] bytes)