Java Binary to Hex binaryToHex(byte[] data)

Here you can find the source of binaryToHex(byte[] data)

Description

binary To Hex

License

Open Source License

Declaration

public static String binaryToHex(byte[] data) 

Method Source Code

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

public class Main {
    private static final char[] HexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f', };

    public static String binaryToHex(byte[] data) {
        return binaryToHex(data, 0, data.length);
    }//from w  w  w  .ja  v a 2s  .  c  o  m

    /**
     * Convert a string of binary bytes to the equivalent hexadecimal string.
     * The resulting String will have two characters for every byte in the
     * input.
     */
    public static String binaryToHex(byte[] data, int offset, int length) {
        assert offset < data.length && offset >= 0 : offset + ", " + data.length;
        int end = offset + length;
        assert end <= data.length && end >= 0 : offset + ", " + length + ", " + data.length;

        char[] chars = new char[length * 2];
        int j = 0;
        for (int i = offset; i < end; i++) {
            int b = data[i];
            chars[j++] = hexDigit(b >>> 4 & 0xF);
            chars[j++] = hexDigit(b & 0xF);
        }

        return new String(chars);
    }

    public static char hexDigit(int i) {
        return HexDigits[i];
    }
}

Related

  1. bin2hex(String bin)
  2. bin2hex(String str)
  3. binaryString2hexString(String bString)
  4. binaryToHex(byte[] ba)
  5. binaryToHex(byte[] bytes)
  6. binaryToHex(char[] binaryStr)
  7. binaryToHex(int binary)
  8. BinaryToHexString(byte[] bytes)
  9. binaryToHexString(String binaryValue)