Java Hex Calculate toHex(final byte[] bytes)

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

Description

to Hex

License

Open Source License

Declaration

public static String toHex(final byte[] bytes) 

Method Source Code

//package com.java2s;
// and/or modify it under the terms of the GNU General Public License 

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

    public static String toHex(final byte[] bytes) {
        return toHex(bytes, true);
    }/*w ww . j a va2 s .c om*/

    public static String toHex(final byte[] bytes, final boolean addSpace) {
        if (bytes == null) {
            return "";
        }
        return toHex(bytes, addSpace, 0, bytes.length);
    }

    public static String toHex(final byte[] bytes, final boolean addSpace, final int index, final int count) {
        if (bytes == null || bytes.length == 0 || count == 0) {
            return "";
        }
        char[] str = new char[count * 3];
        int tmp;
        int len = 0;
        for (int pos = 0; pos != count; ++pos) {
            tmp = bytes[index + pos] & 0xFF;
            str[len] = hexArray[tmp >>> 4];
            ++len;
            str[len] = hexArray[tmp & 0x0F];
            ++len;
            if (addSpace) {
                str[len] = ' ';
                ++len;
            }
        }
        if (addSpace) {
            --len;
        }
        return new String(str, 0, len);
    }
}

Related

  1. toHex(final byte[] bytes)
  2. toHex(final byte[] bytes)
  3. toHex(final byte[] bytes)
  4. toHex(final byte[] bytes)
  5. toHex(final byte[] bytes)
  6. toHex(final byte[] bytes)
  7. tohex(final byte[] bytes)
  8. toHex(final byte[] bytes, final int offset, final int count)
  9. toHex(final byte[] data)