Java Byte Array to String bytesToString(byte[] arr)

Here you can find the source of bytesToString(byte[] arr)

Description

Byte array to string including the 0X notation

License

Open Source License

Parameter

Parameter Description
arr byte[]

Return

String in the format 0x01 0x02 0x03, etc

Declaration

public static String bytesToString(byte[] arr) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w ww  .  ja v a 2  s  . c om*/
     * Byte array to string including the 0X notation
     *
     * @param arr byte[]
     * @return String in the format 0x01 0x02 0x03, etc
     */
    public static String bytesToString(byte[] arr) {
        return bytesToString(arr, null);
    }

    /**
     * Byte array to string including the notation
     *
     * @param arr byte[]
     * @param fmt printf style format. Defaults to "%02X "
     * @return String in the format 0F B1 03, etc
     */
    public static String bytesToString(byte[] arr, String fmt) {

        if (fmt == null || fmt.length() == 0) {
            fmt = "%02X ";
        }

        StringBuilder sb = new StringBuilder(arr.length * 2);
        for (byte b : arr)
            sb.append(String.format(fmt, b & 0xff));
        return sb.toString();

    }
}

Related

  1. bytes2string(byte[] src)
  2. bytesToStr(byte[] bytes)
  3. bytesToStr(byte[] input)
  4. bytesToStrHex(byte[] bytes)
  5. bytesToString(byte... bytes)
  6. bytesToString(byte[] arr, int pos)
  7. bytesToString(byte[] b)
  8. bytesToString(byte[] buffer, int index, int length)
  9. bytesToString(byte[] bytes)