Java Hex Calculate toHex(final byte[] data)

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

Description

Return the passed in byte array as a hex string.

License

Open Source License

Parameter

Parameter Description
data the bytes to be converted.

Return

a hex representation of data.

Declaration

public static String toHex(final byte[] data) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   ww  w .  jav a 2 s.  c o m
     * Return length many bytes of the passed in byte array as a hex string.
     * 
     * @param data the bytes to be converted.
     * @param length the number of bytes in the data block to be converted.
     * @return a hex representation of length bytes of data.
     */
    public static String toHex(final byte[] data, final int length) {
        final StringBuffer buf = new StringBuffer();
        final String digits = "0123456789abcdef";
        for (int i = 0; i != length; i++) {
            final int variName = data[i] & 0xff;//NOCHECKSTYLE '0xff'

            buf.append(digits.charAt(variName >> 4));//NOCHECKSTYLE '4'
            buf.append(digits.charAt(variName & 0xf));//NOCHECKSTYLE '0xf'
        }

        return buf.toString();
    }

    /**
     * Return the passed in byte array as a hex string.
     * 
     * @param data the bytes to be converted.
     * @return a hex representation of data.
     */
    public static String toHex(final byte[] data) {
        return toHex(data, data.length);
    }
}

Related

  1. toHex(final byte[] bytes)
  2. tohex(final byte[] bytes)
  3. toHex(final byte[] bytes, final int offset, final int count)
  4. toHex(final byte[] data)
  5. toHex(final byte[] data)
  6. toHex(final byte[] data, final int startPos, final int length)
  7. toHex(final byte[] data, final String separator)
  8. toHex(final byte[] input)
  9. toHex(final byte[] value)