Java Hex Calculate toHexString(byte[] bytes)

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

Description

Given an array of bytes, it will return the representation as a string of hex data.

License

Open Source License

Parameter

Parameter Description
bytes - the bytes to convert to hex

Return

a hex representation of the given byte array bytes

Declaration

public static String toHexString(byte[] bytes) 

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' };

    /**/*from   www. j  av  a 2  s  .c om*/
     * <p>Given an array of bytes, it will return the representation as a string of hex data.
     * This is primarily useful when needing to display encrypted data as a readable string.</p>
     * 
     * @param bytes - the bytes to convert to hex
     * @return a hex representation of the given byte array <code>bytes</code>
     */
    public static String toHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            sb.append(new char[] { HEX_CHARS[(b >> 4) & 0x0f], HEX_CHARS[b & 0x0f] });
        }

        return sb.toString();
    }
}

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)