Java Hex Calculate toHex(byte[] data)

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

Description

to Hex

License

BSD License

Declaration

public static String toHex(byte[] data) 

Method Source Code

//package com.java2s;
/**/*from   w  w  w . j  a  va  2s  .com*/
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */

public class Main {
    /**
     * Reasonably efficient Hex checksum converter
     * 
     * @param data
     *        byte array
     * @return hexString
     *        checksum
     */
    static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    public static String toHex(byte[] data) {
        if ((data == null) || (data.length == 0)) {
            return null;
        }
        char[] chars = new char[2 * data.length];
        for (int i = 0; i < data.length; ++i) {
            chars[2 * i] = HEX_CHARS[(data[i] & 0xF0) >>> 4];
            chars[2 * i + 1] = HEX_CHARS[data[i] & 0x0F];
        }
        return new String(chars);
    }
}

Related

  1. toHex(byte[] data)
  2. toHex(byte[] data)
  3. toHex(byte[] data)
  4. toHex(byte[] data)
  5. toHex(byte[] data)
  6. toHex(byte[] data)
  7. toHex(byte[] data, int bytesPerGroup)
  8. toHex(byte[] data, int length)
  9. toHex(byte[] data, int off, int len)