Java Byte Array to String bytesToString(final byte[] arr, final boolean signed, final boolean littleEndian, final String sep)

Here you can find the source of bytesToString(final byte[] arr, final boolean signed, final boolean littleEndian, final String sep)

Description

Returns a string view of a byte array

License

Apache License

Parameter

Parameter Description
arr the given byte array
signed set true if you want the byte values signed.
littleEndian set true if you want Little-Endian order
sep the separator string between bytes

Return

a string view of a byte array

Declaration

public static String bytesToString(final byte[] arr, final boolean signed, final boolean littleEndian,
        final String sep) 

Method Source Code

//package com.java2s;
/*//from   ww w  . j  a  va2 s  .  co m
 * Copyright 2015-16, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */

public class Main {
    /**
     * Returns a string view of a byte array
     * @param arr the given byte array
     * @param signed set true if you want the byte values signed.
     * @param littleEndian set true if you want Little-Endian order
     * @param sep the separator string between bytes
     * @return a string view of a byte array
     */
    public static String bytesToString(final byte[] arr, final boolean signed, final boolean littleEndian,
            final String sep) {
        final StringBuilder sb = new StringBuilder();
        final int mask = (signed) ? 0XFFFFFFFF : 0XFF;
        final int arrLen = arr.length;
        if (littleEndian) {
            for (int i = 0; i < arrLen - 1; i++) {
                sb.append(arr[i] & mask).append(sep);
            }
            sb.append(arr[arrLen - 1] & mask);
        } else {
            for (int i = arrLen; i-- > 1;) {
                sb.append(arr[i] & mask).append(sep);
            }
            sb.append(arr[0] & mask);
        }
        return sb.toString();
    }
}

Related

  1. bytesToString(byte[] id)
  2. bytesToString(byte[] input)
  3. bytesToString(byte[] myByte)
  4. bytesToString(byte[] value)
  5. bytesToString(char[] bytes, boolean le, boolean signed)
  6. bytesToString(final byte[] bytes)
  7. bytesToString(final byte[] data)
  8. bytesToString(int bytes)
  9. bytesToString(int bytes)