Java Hex Calculate toHexString(byte[] bytes)

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

Description

Converts an array of bytes to an Hexadecimal String

License

Apache License

Parameter

Parameter Description
bytes the array of bytes

Return

the Hexadecimal String

Declaration

public static String toHexString(byte[] bytes) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static char[] HEXES = { '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    /**//  w w w  .  j a  v a  2s  .  co  m
     * Converts an array of bytes to an Hexadecimal String
     * 
     * @param bytes
     *          the array of bytes
     * @return the Hexadecimal String
     */
    public static String toHexString(byte[] bytes) {

        StringBuffer stringBuffer = new StringBuffer(3 * bytes.length + 2);

        for (int i = 0; i < bytes.length; i++) {

            int c = bytes[i];

            if (c < 0) {

                c += 256;
            }

            stringBuffer.append(HEXES[c / 16]);
            stringBuffer.append(HEXES[c % 16]);
        }

        return stringBuffer.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)