Java Hex Calculate toHexString(byte[] bytes)

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

Description

Returns a string representings the hexadecimal values of the bytes.

License

Open Source License

Parameter

Parameter Description
bytes the bytes array

Return

String of hexadecimal representing the bytes

Declaration

public static final String toHexString(byte[] bytes) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from www .  j a  v  a 2  s. co m*/
     * Returns a string representings the hexadecimal values of the bytes. If the
     * bytes supplied is null or an empty array, then an empty string in returned.
     * 
     * @param bytes the bytes array
     * @return String of hexadecimal representing the bytes
     */
    public static final String toHexString(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        }

        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            if (((int) bytes[i] & 0xff) < 0x10) {
                buffer.append("0");
            }

            buffer.append(Long.toString((int) bytes[i] & 0xff, 16));
        }

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