Java Byte Array to Hex String bytesToHexString(byte[] array)

Here you can find the source of bytesToHexString(byte[] array)

Description

Converts the given byte array to a hexadecimal string that can be converted back by #hexStringToBytes(String) .

License

Apache License

Parameter

Parameter Description
array the array of bytes to convert

Return

the hexadecimal string

Declaration

public static String bytesToHexString(byte[] array) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

    /**//from w  w  w.j  ava  2s.  c o  m
     * Converts the given byte array to a hexadecimal string that can be converted
     * back by {@link #hexStringToBytes(String)}.
     * 
     * @param array the array of bytes to convert
     * @return the hexadecimal string
     */
    public static String bytesToHexString(byte[] array) {
        StringBuilder sb = new StringBuilder(array.length * 2);
        for (int i = 0; i < array.length; ++i) {
            sb.append(HEX_CHARS[(array[i] >>> 4) & 0xF]);
            sb.append(HEX_CHARS[array[i] & 0xF]);
        }
        return sb.toString();
    }
}

Related

  1. bytesToHexString(byte abyte0[])
  2. bytesToHexString(byte abyte0[], int i, int j)
  3. bytesToHexString(byte bytes[])
  4. bytesToHexString(byte data[])
  5. bytesToHexString(byte[] _bytes)
  6. bytesToHexString(byte[] b)
  7. bytesToHexString(byte[] b, int length)
  8. bytesToHexString(byte[] bArray)
  9. bytesToHexString(byte[] bArray)