Java Hex Calculate toHex(byte[] data, int length)

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

Description

Return length many bytes of the passed in byte array as a hex string.

License

Open Source License

Parameter

Parameter Description
data the bytes to be converted.
length the number of bytes in the data block to be converted.

Return

a hex representation of length bytes of data.

Declaration

public static String toHex(byte[] data, int length) 

Method Source Code

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

public class Main {
    private static String digits = "0123456789abcdef";

    /**//from   ww w  .  j av a2  s . c om
     * 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(byte[] data, int length) {
        StringBuffer buf = new StringBuffer();

        for (int i = 0; i != length; i++) {
            int v = data[i] & 0xff;

            buf.append(digits.charAt(v >> 4));
            buf.append(digits.charAt(v & 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(byte[] data) {
        return toHex(data, data.length).toUpperCase();
    }
}

Related

  1. toHex(byte[] data)
  2. toHex(byte[] data)
  3. toHex(byte[] data)
  4. toHex(byte[] data)
  5. toHex(byte[] data, int bytesPerGroup)
  6. toHex(byte[] data, int off, int len)
  7. toHex(byte[] data, int perLine, boolean offset)
  8. toHex(byte[] dBytes)
  9. toHex(byte[] digest)