Java Hex Calculate toHexString(byte[] bytes)

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

Description

Converts a byte array to a hexadecimal ASCII string.

License

Open Source License

Parameter

Parameter Description
bytes The byte array

Return

The string representing the byte array

Declaration

public static String toHexString(byte[] bytes) 

Method Source Code

//package com.java2s;

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

    /**//from w  w w  .ja  va2 s  .c o  m
     * Converts a byte array to a hexadecimal ASCII string.
     * The string will be in upper case and does not start with '0x'.
     * This method is the reverse of <code>toBytes</code>.
     *
     * @param bytes The byte array
     * @return The string representing the byte array
     * @see #toBytes(String)
     */
    public static String toHexString(byte[] bytes) {
        String str = "";
        if (bytes != null) {
            int len = bytes.length;
            for (int i = 0; i < len; i++) {
                str += toHex(bytes[i]);
            }
        }
        return str;
    }

    /**
     * Converts one int to a hexadecimal ASCII string.
     *
     * @param val The integer
     * @return The hex string
     */
    public static String toHex(int val) {
        int val1, val2;

        val1 = (val >> 4) & 0x0F;
        val2 = (val & 0x0F);

        return ("" + HEX_DIGIT[val1] + HEX_DIGIT[val2]);
    }
}

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)