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

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

Description

bytes To Hex String

License

Apache License

Declaration

public static String bytesToHexString(byte[] src) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String bytesToHexString(byte[] src) {

        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }//from   w  w w.  j av  a  2s. c o  m
        for (int i = 0; i < src.length; i++) {

            int v = src[i] & 0xFF;

            String hv = Integer.toHexString(v);

            if (hv.length() < 2) {

                stringBuilder.append(0);

            }

            stringBuilder.append(hv);

        }
        String s = stringBuilder.toString();

        StringBuilder s2 = new StringBuilder("");

        for (int i = 0; i < s.length(); i++) {
            if (i != 0 && i % 2 == 0) {
                s2.append(" ");
            }
            s2.append(s.charAt(i));

        }

        stringBuilder = null;

        return s2.toString();
    }
}

Related

  1. bytesToHexString(byte[] data, int offset, int length)
  2. bytesToHexString(byte[] hasher)
  3. bytesToHexString(byte[] in, int length)
  4. bytesToHexString(byte[] input)
  5. bytesToHexString(byte[] mpi)
  6. bytesToHexString(byte[] src)
  7. bytesToHexString(final byte[] bytes)
  8. bytesToHexString(final byte[] bytes)
  9. bytesToHexString(final byte[] bytes)