Java Byte Array to Hex bytesToHex(byte... bytes)

Here you can find the source of bytesToHex(byte... bytes)

Description

Convert bytes to HEX values in a string ("null safe").

License

Open Source License

Parameter

Parameter Description
bytes Bytes to convert.

Return

The result string.

Declaration

public static String bytesToHex(byte... bytes) 

Method Source Code

//package com.java2s;
/*/*from   www.j a  v  a  2  s .c o  m*/
 * Copyright (C) 2015 J.S. TESSIER
 * 
 * This file is part of java-rf24.
 * 
 * java-rf24 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * java-rf24 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with java-rf24. If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
 */

public class Main {
    /**
     * Convert bytes to HEX values in a string ("null safe").
     * 
     * @param bytes   Bytes to convert.
     * @return   The result string.
     */
    public static String bytesToHex(byte... bytes) {
        if (bytes == null) {
            return null;
        }
        final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] hexChars = new char[bytes.length * 2];
        for (int i = 0; i < bytes.length; i++) {
            hexChars[i * 2] = hexArray[(Byte.toUnsignedInt(bytes[i]) >>> 4)];
            hexChars[i * 2 + 1] = hexArray[bytes[i] & 0x0F];
        }
        return new String(hexChars);
    }
}

Related

  1. bytes2HexString(byte[] bytes)
  2. bytes2HexString(byte[] src)
  3. bytes2HexStringWithSeparator(String separator, byte... bytes)
  4. bytesToHex(byte bytes[], int offset, int length, boolean wrap)
  5. bytesToHex(byte in[])
  6. bytesToHex(byte[] a)
  7. bytesToHex(byte[] a)
  8. bytesToHex(byte[] array)
  9. bytesToHex(byte[] b)