Java Hex Calculate toHexArray(byte[] binary)

Here you can find the source of toHexArray(byte[] binary)

Description

Turns the binary array to a hexadecimal string.

License

Open Source License

Parameter

Parameter Description
binary the array to convert

Return

the hex string

Declaration

public static String toHexArray(byte[] binary) 

Method Source Code

//package com.java2s;
/*// w w w  .j  a  v  a2s. c o  m
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /** hexadecimal digits. */
    public static final char HEX_DIGIT[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    /**
     * Turns the binary array to a hexadecimal string.
     *
     * @param binary   the array to convert
     * @return      the hex string
     */
    public static String toHexArray(byte[] binary) {
        StringBuilder result;

        result = new StringBuilder();
        for (byte b : binary)
            result.append(toHex(b));

        return result.toString();
    }

    /**
     * Returns a hexadecimal representation of the byte value.
     * <br><br>
     * Taken from <a href="http://www.herongyang.com/Cryptography/SHA1-Message-Digest-in-Java.html" target="_blank">here</a>.
     *
     * @param value   the value to convert
     * @return      the hexadecimal representation
     */
    public static String toHex(byte value) {
        StringBuilder result;

        result = new StringBuilder();
        result.append(HEX_DIGIT[(value >> 4) & 0x0f]);
        result.append(HEX_DIGIT[(value) & 0x0f]);

        return result.toString();
    }
}

Related

  1. toHexadecimal(byte[] digest)
  2. toHexadecimal(final byte[] array)
  3. toHexadecimal(final byte[] in)
  4. toHexadecimal(final byte[] message)
  5. toHexadecimealString(byte[] data, int length, boolean uppercase)
  6. toHexaString(byte[] data)
  7. toHexaString(int value)
  8. toHexByte(byte b)
  9. toHexByte(byte b, StringBuffer sb)