Here you can find the source of bytesToString(final byte[] arr, final boolean signed, final boolean littleEndian, final String sep)
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 |
public static String bytesToString(final byte[] arr, final boolean signed, final boolean littleEndian, final String sep)
//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(); } }