Java Byte Array to Hex bufferToHexString(byte[] buffer)

Here you can find the source of bufferToHexString(byte[] buffer)

Description

Convert a byte buffer to a hexadecimal string.

License

GNU General Public License

Parameter

Parameter Description
buffer byte buffer

Return

hexadecimal string

Declaration

public static String bufferToHexString(byte[] buffer) 

Method Source Code

//package com.java2s;
/*/* www  . j a  va 2  s. c om*/
 * Copyright (C) 2002-2017 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

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' };

    /**
     * Convert a byte buffer to a hexadecimal string.
     * @param buffer byte buffer
     * @return hexadecimal string
     */
    public static String bufferToHexString(byte[] buffer) {
        StringBuffer sb = new StringBuffer(buffer.length * 2);
        for (int i = 0; i < buffer.length; i++) {
            char a = HEX_CHARS[(buffer[i] & 0xF0) >> 4];
            char b = HEX_CHARS[buffer[i] & 0x0F];
            sb.append(a);
            sb.append(b);
        }
        return sb.toString();
    }
}

Related

  1. bufferToHex(byte bytes[])
  2. bufferToHexString(byte[] data, int start, int length)
  3. bytes2Hex(byte bt)
  4. bytes2Hex(byte... values)
  5. bytes2Hex(byte[] abValue)