Java Hex Calculate toHexString(byte[] input)

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

Description

Convert a byte array to the corresponding hexstring.

License

Open Source License

Parameter

Parameter Description
input the byte array to be converted

Return

the corresponding hexstring

Declaration

public static String toHexString(byte[] input) 

Method Source Code

//package com.java2s;

public class Main {
    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**/*  w w w  . ja  v  a 2 s.co m*/
     * Convert a byte array to the corresponding hexstring.
     *
     * @param input the byte array to be converted
     * @return the corresponding hexstring
     */
    public static String toHexString(byte[] input) {
        String result = "";
        for (int i = 0; i < input.length; i++) {
            result += HEX_CHARS[(input[i] >>> 4) & 0x0f];
            result += HEX_CHARS[(input[i]) & 0x0f];
        }
        return result;
    }

    /**
     * Convert a byte array to the corresponding hex string.
     *
     * @param input     the byte array to be converted
     * @param prefix    the prefix to put at the beginning of the hex string
     * @param seperator a separator string
     * @return the corresponding hex string
     */
    public static String toHexString(byte[] input, String prefix,
            String seperator) {
        String result = new String(prefix);
        for (int i = 0; i < input.length; i++) {
            result += HEX_CHARS[(input[i] >>> 4) & 0x0f];
            result += HEX_CHARS[(input[i]) & 0x0f];
            if (i < input.length - 1) {
                result += seperator;
            }
        }
        return result;
    }
}

Related

  1. toHexString(byte[] digest)
  2. toHexString(byte[] digest)
  3. toHexString(byte[] digest)
  4. toHexString(byte[] digestByte)
  5. toHexString(byte[] in)
  6. toHexString(byte[] md)
  7. toHexString(byte[] messagePayload)
  8. toHexString(byte[] paramArrayOfByte)
  9. toHexString(byte[] paramArrayOfByte, String paramString, boolean paramBoolean)