Java Hex Calculate toHexString(byte[] bytes)

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

Description

Creates a hexadecimal representation of all given bytes an concatenates these hex-values to a single string.

License

Open Source License

Parameter

Parameter Description
bytes the byte values to convert, cannot be <code>null</code>.

Return

a hex-string of the given bytes, never null.

Declaration

public static String toHexString(byte[] bytes) 

Method Source Code

//package com.java2s;
/**//from   www  .jav  a2  s  . c o  m
 * Licensed under Apache License v2. See LICENSE for more information.
 */

public class Main {
    /** The hexadecimal alphabet. */
    private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();

    /**
     * Creates a hexadecimal representation of all given bytes an concatenates these
     * hex-values to a single string.
     * 
     * @param bytes the byte values to convert, cannot be <code>null</code>.
     * @return a hex-string of the given bytes, never <code>null</code>.
     */
    public static String toHexString(byte[] bytes) {
        // based on <http://stackoverflow.com/a/9855338/229140>
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for (int j = 0; j < bytes.length; j++) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_DIGITS[v >>> 4];
            hexChars[j * 2 + 1] = HEX_DIGITS[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Related

  1. toHexString(byte[] bytes)
  2. toHexString(byte[] bytes)
  3. toHexString(byte[] bytes)
  4. toHexString(byte[] bytes)
  5. toHexString(byte[] bytes)
  6. toHexString(byte[] bytes)
  7. toHexString(byte[] bytes)
  8. toHexString(byte[] bytes)
  9. toHexString(byte[] bytes)