Java Hex Calculate toHexString(final byte[] raw)

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

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(final byte[] raw) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

    public static String toHexString(final byte[] raw) {
        return toHexString(raw, 0, raw.length);
    }/*w  ww  . ja v a 2  s.  co m*/

    public static String toHexString(final byte[] raw, final int offset, final int length) {
        final byte[] hex = new byte[2 * length];
        int index = 0;
        for (int i = 0; i < length; i++) {
            final int v = raw[i + offset] & 0xFF;
            hex[index++] = HEX_CHAR_TABLE[v >>> 4];
            hex[index++] = HEX_CHAR_TABLE[v & 0xF];
        }
        return new String(hex);
    }

    public static String toHexString(final int value, final int minDigits) {
        String hexString = Integer.toHexString(value);
        for (int i = minDigits - hexString.length(); i > 0; i--) {
            hexString = "0" + hexString;
        }

        return hexString.toUpperCase();
    }
}

Related

  1. toHexString(final byte[] bytes)
  2. toHexString(final byte[] data)
  3. toHexString(final byte[] data)
  4. toHexString(final byte[] data)
  5. toHexString(final byte[] fieldData)
  6. toHexString(final int i)
  7. toHexString(final int value)
  8. toHexString(final long num, final char paddingChar, int min, int max)
  9. toHexString(int b)