Android Byte Array to Hex Convert 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

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

    /**//  ww  w .j a  va 2  s  .  c o  m
     * <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 {@link String}
     */
    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. byteArrayToHexString(byte[] b)
  2. toHexString(byte abyte0[], int beginIndex, int endIndex, boolean spaceFlag)
  3. toHexString(byte abyte0[], int beginIndex, int endIndex)
  4. toHexString(byte abyte0[], boolean spaceFlag)
  5. toHexString(byte abyte0[])
  6. encodeHexStr(final byte[] bytes)
  7. byteArrayToHexString(byte[] b)
  8. getHexString(byte[] raw)
  9. byte2hex(byte[] b)