Android Byte Array to Hex Convert toHex(byte[] dataBytes)

Here you can find the source of toHex(byte[] dataBytes)

Description

Encode a byte array to a hexadecimal string.

License

Open Source License

Parameter

Parameter Description
dataBytes bytearray

Return

hexadecimal string.

Declaration

public static String toHex(byte[] dataBytes)
        throws NumberFormatException 

Method Source Code

//package com.java2s;
import java.math.BigInteger;

public class Main {
    /**/*from w w w.j ava 2s . com*/
     * Encode a byte array to a hexadecimal string.
     * 
     * @param dataBytes bytearray
     * @return hexadecimal string.
     */
    public static String toHex(byte[] dataBytes)
            throws NumberFormatException {
        if (dataBytes == null || dataBytes.length == 0) {
            return "";
        }
        byte[] dataPlus1Bytes = new byte[dataBytes.length + 1];
        System.arraycopy(dataBytes, 0, dataPlus1Bytes, 1, dataBytes.length);
        dataPlus1Bytes[0] = 1;
        BigInteger dataPlus1BigInteger = new BigInteger(dataPlus1Bytes);
        return dataPlus1BigInteger.toString(16).substring(1);
    }
}

Related

  1. toHex(byte[] bytes)
  2. toHex(byte[] bytes)
  3. toHex(byte[] bytes, String separator)
  4. toHex(byte[] bytes, int offset, int length)
  5. toHex(byte[] bytes, int offset, int length, String separator)
  6. toHexString(byte[] ba)
  7. toHexString(byte[] bytes)
  8. toHexString(byte[] bytes)
  9. toHexString(byte[] bytes)