Java Byte Array to Hex String bytes2hexStr(byte[] arr, int len)

Here you can find the source of bytes2hexStr(byte[] arr, int len)

Description

Function: bytes2hexStr (byte [] arr, int len)( Generate a hex string showing the value of the bytes in ARR.

License

Open Source License

Declaration

public final static String bytes2hexStr(byte[] arr, int len) 

Method Source Code

//package com.java2s;

public class Main {
    static final char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**//from  www  . j ava 2 s  . c  om
     * Function: <tt>bytes2hexStr (byte [] arr, int len)(</tt>
     * Generate a hex string showing the value of the bytes in ARR.
     * (Used by me for testing this class.)
     */
    public final static String bytes2hexStr(byte[] arr, int len) {
        StringBuffer sb = new StringBuffer(len * 2);
        for (int i = 0; i < len; i++) {
            int hi = (arr[i] >>> 4) & 0xf;
            sb.append(hex[hi]);
            int low = (arr[i]) & 0xf;
            sb.append(hex[low]);
        }
        return sb.toString();
    }

    public static String bytes2hexStr(byte[] arr) {
        return bytes2hexStr(arr, arr.length);
    }
}

Related

  1. bytes_to_hex(byte[] b)
  2. bytes_to_hex(byte[] bytes)
  3. bytesToHexAppend(byte[] bs, int off, int length, StringBuffer sb)
  4. bytesToHexChars(byte[] bytes)