Java Hex Calculate toHex(byte[] data, int bytesPerGroup)

Here you can find the source of toHex(byte[] data, int bytesPerGroup)

Description

to Hex

License

Open Source License

Declaration

public static String toHex(byte[] data, int bytesPerGroup) 

Method Source Code

//package com.java2s;

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

    public static String toHex(byte data) {
        return "" + HEX[(data >> 4) & 0xf] + HEX[data & 0xf];
    }/*ww  w  .ja v a 2s. com*/

    public static String toHex(byte[] data) {
        char[] buf = new char[data.length * 2];
        for (int i = 0, j = 0, n = data.length; i < n; i++, j += 2) {
            buf[j] = HEX[(data[i] >> 4) & 0xf];
            buf[j + 1] = HEX[data[i] & 0xf];
        }
        return new String(buf);
    }

    public static String toHex(byte[] data, int bytesPerGroup) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0, n = data.length; i < n; i++) {
            if ((i % bytesPerGroup) == 0 && i > 0) {
                sb.append(' ');
            }
            sb.append(HEX[(data[i] >> 4) & 0xf]);
            sb.append(HEX[data[i] & 0xf]);
        }
        return sb.toString();
    }
}

Related

  1. toHex(byte[] data)
  2. toHex(byte[] data)
  3. toHex(byte[] data)
  4. toHex(byte[] data)
  5. toHex(byte[] data)
  6. toHex(byte[] data, int length)
  7. toHex(byte[] data, int off, int len)
  8. toHex(byte[] data, int perLine, boolean offset)
  9. toHex(byte[] dBytes)