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

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

Description

Converts a byte array into its hex string representation.

License

Open Source License

Parameter

Parameter Description
bytes Bytes to convert

Return

Hex string of delivered byte array

Declaration

public static String bytesToHex(final byte[] bytes) 

Method Source Code

//package com.java2s;
/**/*from  w  w w  .  j av a 2  s .  c om*/
 * WS-Attacker - A Modular Web Services Penetration Testing Framework Copyright
 * (C) 2013 Dennis Kupser
 *
 * 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 2 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, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

public class Main {
    /**
     * Valid Hex Chars.
     */
    private final static char[] HEXCHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    /**
     * Converts a byte array into its hex string representation.
     * 
     * @param bytes Bytes to convert
     * @return Hex string of delivered byte array
     */
    public static String bytesToHex(final byte[] bytes) {
        StringBuilder builder = new StringBuilder(bytes.length * 2);

        for (int i = 0; i < bytes.length; i++) {
            // unsigned right shift of the MSBs
            builder.append(HEXCHARS[(bytes[i] & 0xff) >>> 4]);
            // handling the LSBs
            builder.append(HEXCHARS[bytes[i] & 0xf]);
            builder.append(' ');
        }

        return builder.toString();
    }
}

Related

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