Java Hex Calculate toHex(byte[] raw)

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

Description

to Hex

License

Apache License

Declaration

public static String toHex(byte[] raw) 

Method Source Code

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

public class Main {
    static final String HEXES = "0123456789abcdef";

    public static String toHex(byte[] raw) {
        if (raw == null) {
            return null;
        }//from   ww w  .j a  va  2s .c  om
        int index = 0;
        final StringBuilder hex = new StringBuilder(2 * raw.length);
        for (final byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));

            if (index < raw.length - 1) {
                hex.append(" ");
            }

            index++;
        }
        return hex.toString();
    }

    /**
     * Try and provide some nice formatting for this object
     */
    private static String toString(Object fieldValue) {
        String toString;

        if (fieldValue instanceof byte[]) {
            toString = toHex((byte[]) fieldValue);
        } else {
            toString = fieldValue.toString();
        }

        return toString;
    }
}

Related

  1. toHex(byte[] digest)
  2. toHex(byte[] inBytes)
  3. toHex(byte[] input)
  4. toHex(byte[] key)
  5. toHex(byte[] raw)
  6. toHex(byte[] raw)
  7. ToHex(byte[] src)
  8. toHex(byte[] src)
  9. toHex(byte[] text)