package com.example.imlinkclient.utils;

public class DataConvertUtils {

    /**
     * 把byte数组转化为二进制样式的字符串
    * @param array
     * @return
     */
    public static String toBinaryString(byte[] array) {
        if (array == null) {
            return "null";
        }
        if (array.length == 0) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(array[0]);
        for (int i = 1; i < array.length; i++) {
            sb.append(", ");
            sb.append(Integer.toBinaryString(array[i]));
        }
        sb.append(']');
        return sb.toString();
    }


    /**
     * 把bytes后面的0去掉
     * @param bytes
     * @return
     */
    public static byte[] getRealBytes(byte[] bytes) {
        int length = 0;
        for (int i = bytes.length-1; i >= 0; i--) {
            if (bytes[i] != 0) {
                length = i;
                break;
            }
        }
        length += 1;
        byte[] realBytes = new byte[length];
        System.arraycopy(bytes, 0, realBytes, 0, length);
        return realBytes;
    }
}
