Java Binary to Hex bin2Hex(byte[] bin)

Here you can find the source of bin2Hex(byte[] bin)

Description

Binary byte[] to Heximal byte[]

License

Open Source License

Parameter

Parameter Description
bin - data in binary for convert

Declaration

public static byte[] bin2Hex(byte[] bin) 

Method Source Code

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

public class Main {
    /**/*from w  ww.j  a  v a 2s .co  m*/
     * Binary byte[] to Heximal byte[]
     * 
     * @param bin
     *            - data in binary for convert
     */
    public static byte[] bin2Hex(byte[] bin) {
        // Allocate byte[] for return
        byte[] hex = new byte[2 * bin.length];

        // Convert one binary byte to two heximal bytes
        for (int i = 0, j = 0; i < bin.length; i++, j += 2) {
            int iByte = bin[i];
            if (iByte < 0)
                iByte += 256;
            // First byte
            int i4Bit = iByte >> 4;
            if (i4Bit < 10)
                hex[j] = (byte) ('0' + i4Bit);
            else
                hex[j] = (byte) ('A' + i4Bit - 10);
            // Second byte
            i4Bit = iByte & 0x000F;
            if (i4Bit < 10)
                hex[j + 1] = (byte) ('0' + i4Bit);
            else
                hex[j + 1] = (byte) ('A' + i4Bit - 10);
        }

        // Return the heximal byte[]
        return hex;
    }
}

Related

  1. bin2hex(byte[] arr)
  2. bin2Hex(byte[] b)
  3. bin2hex(final byte[] b)
  4. bin2hex(final byte[] base)
  5. bin2hex(int digit)
  6. bin2Hex(String bin)