Java Byte Array to Hex bytesToHex(byte[] binary)

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

Description

Convert binary array to a hex string.

License

Open Source License

Parameter

Parameter Description
binary a parameter

Declaration

public static String bytesToHex(byte[] binary) 

Method Source Code

//package com.java2s;
/***********************************************************************
* Copyright (c) 2015 by Regents of the University of Minnesota.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, Version 2.0 which 
* accompanies this distribution and is available at
* http://www.opensource.org/licenses/apache2.0.php.
*
*************************************************************************/

public class Main {
    private static final byte[] HexLookupTable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
            'D', 'E', 'F' };

    /**//from  ww  w  .j  av  a  2 s  .c  o m
     * Convert binary array to a hex string.
     * @param binary
     * @return
     */
    public static String bytesToHex(byte[] binary) {
        // Each byte is converted to two hex values
        byte[] hex = new byte[binary.length * 2];
        for (int i = 0; i < binary.length; i++) {
            hex[2 * i] = HexLookupTable[(binary[i] & 0xFF) >>> 4];
            hex[2 * i + 1] = HexLookupTable[binary[i] & 0xF];
        }
        return new String(hex);
    }
}

Related

  1. bytesToHex(byte[] b)
  2. bytesToHex(byte[] b)
  3. bytesToHex(byte[] b)
  4. bytesToHex(byte[] b)
  5. bytesToHex(byte[] b, int offset, int length)
  6. bytesToHex(byte[] bs, int off, int length)
  7. bytesToHex(byte[] bt)
  8. bytesToHex(byte[] buf)
  9. bytesToHex(byte[] bytes)