Java Byte Array to Hex bytesToHex(final byte[] bytes)

Here you can find the source of bytesToHex(final byte[] bytes)

Description

Create a hexadecimal string representation of the given byte array.

License

Open Source License

Declaration

public static String bytesToHex(final byte[] bytes) 

Method Source Code

//package com.java2s;
/*/*from   w w  w.  j a v a 2s.c o m*/
 * Copyright Nathan Jones 2013
 *
 * This file is part of Juzidian.
 *
 * Juzidian 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.
 *
 * Juzidian 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 Juzidian.  If not, see <http://www.gnu.org/licenses/>.
 */

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

    /**
     * Create a hexadecimal string representation of the given byte array.
     */
    public static String bytesToHex(final byte[] bytes) {
        final StringBuilder sb = new StringBuilder();
        for (final byte b : bytes) {
            sb.append(byteToHex(b));
        }
        return sb.toString();
    }

    private static String byteToHex(final byte b) {
        final char byteHexChar1 = HEX_CHARS[(b & 0xf0) >> 4];
        final char byteHexChar2 = HEX_CHARS[b & 0x0f];
        return byteHexChar1 + "" + byteHexChar2;
    }
}

Related

  1. bytesToHex(byte[] raw)
  2. bytesToHex(byte[] raw)
  3. bytesToHex(byte[] raw)
  4. bytesToHex(final byte[] b)
  5. bytesToHex(final byte[] bytes)
  6. bytesToHex(final byte[] bytes)
  7. bytesToHex(final byte[] bytes)
  8. bytesToHex(final byte[] bytes, final boolean toLowerCase)
  9. bytesToHex(final byte[] bytes, final int position, final int length)