Java Utililty Methods Byte Array to Hex String

List of utility methods to do Byte Array to Hex String

Description

The list of methods to do Byte Array to Hex String are organized into topic(s).

Method

StringbytesToHexString(byte[] data, int fromIndex, int toIndex)
bytes To Hex String
StringBuilder sb = new StringBuilder();
for (int i = fromIndex; i < toIndex; i++) {
    sb.append(hex[(data[i] >> 4) & 0xf]);
    sb.append(hex[data[i] & 0xf]);
return sb.toString();
StringbytesToHexString(byte[] data, int offset, int length)
bytes To Hex String
StringBuffer s = new StringBuffer();
for (int i = offset; i < offset + length; i++) {
    int b = data[i] >= 0 ? data[i] : 0x0100 + data[i];
    s.append(HEX_DIGITS[b / 16]).append(HEX_DIGITS[b % 16]);
return s.toString();
StringbytesToHexString(byte[] hasher)
This method used for converting byte array to hex string
String HEXES = "0123456789abcdef";
StringBuilder hex = new StringBuilder(2 * hasher.length);
for (final byte b : hasher) {
    hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
return hex.toString();
char[]bytesToHexString(byte[] in, int length)
bytes To Hex String
if (length > in.length)
    return null;
char[] out = new char[length * 2];
for (int i = 0; i < length; i++) {
    byte b = in[i];
    out[i * 2] = hex[(b >>> 4) & 0xf];
    out[i * 2 + 1] = hex[b & 0xf];
return out;
StringbytesToHexString(byte[] input)
bytes To Hex String
return bytesToHexString(input, input.length);
StringbytesToHexString(byte[] mpi)
bytes To Hex String
byte[] hex = new byte[2 * mpi.length];
for (int i = 0; i < mpi.length; i++) {
    int num = (int) (mpi[i] >> 4 & 0xf);
    if (num <= 9) {
        hex[2 * i] = (byte) ('0' + num);
    } else {
        hex[2 * i] = (byte) ('A' + num - 10);
    num = (int) (mpi[i] & 0xf);
    if (num <= 9) {
        hex[2 * i + 1] = (byte) ('0' + num);
    } else {
        hex[2 * i + 1] = (byte) ('A' + num - 10);
return new String(hex);
StringbytesToHexString(byte[] src)
bytes To Hex String
if (src == null || src.length <= 0) {
    return "";
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < src.length; i++) {
    String hv = Integer.toHexString(src[i] & 0xFF);
    if (hv.length() < 2) {
        stringBuilder.append(0);
...
StringbytesToHexString(byte[] src)
bytes To Hex String
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
    return null;
for (int i = 0; i < src.length; i++) {
    int v = src[i] & 0xFF;
    String hv = Integer.toHexString(v);
    if (hv.length() < 2) {
...
StringbytesToHexString(final byte[] bytes)
bytes To Hex String
checkNotNull(bytes, "bytes");
final char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
    final int v = bytes[j] & 0xFF;
    hexChars[j * 2] = HEX_ARRAY[v >>> 4];
    hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
return new String(hexChars);
...
StringbytesToHexString(final byte[] bytes)
Converts the specified bytes array into its hex string representation.
if (bytes == null)
    return null;
char[] chars = new char[bytes.length * 2];
for (int byteIndex = 0, charIndex = 0; byteIndex < bytes.length; ++byteIndex, charIndex += 2) {
    String str = Integer.toString(bytes[byteIndex] + 128, 16);
    str = ensureLength(str, 2, false, '0');
    chars[charIndex] = str.charAt(0);
    chars[charIndex + 1] = str.charAt(1);
...