package com.dowse.base.log;

import android.text.TextUtils;
import com.dowse.base.param.device.DeviceConst;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/log/ByteTools.class */
public class ByteTools {
    public static float bytesToFloat(byte[] b, int index) {
        int l = b[index + 3] << 0;
        return Float.intBitsToFloat((int) ((((int) ((((int) ((l & 255) | (b[index + 2] << 8))) & 65535) | (b[index + 1] << 16))) & 16777215) | (b[index + 0] << 24)));
    }

    public static int bytesToInt(List<Byte> bytes) {
        if (bytes == null || bytes.size() == 0) {
            return 0;
        }
        if (bytes.size() == 1) {
            return bytes.get(0).byteValue();
        }
        if (bytes.size() == 2) {
            return (65280 & (bytes.get(1).byteValue() << 8)) | (255 & bytes.get(0).byteValue());
        }
        if (bytes.size() == 3) {
            return (16711680 & (bytes.get(2).byteValue() << 16)) | (65280 & (bytes.get(1).byteValue() << 8)) | (255 & bytes.get(0).byteValue());
        }
        if (bytes.size() == 4) {
            return ((-16777216) & (bytes.get(3).byteValue() << 24)) | (16711680 & (bytes.get(2).byteValue() << 16)) | (65280 & (bytes.get(1).byteValue() << 8)) | (255 & bytes.get(0).byteValue());
        }
        return 0;
    }

    public static int bytesToIntBigEndian(List<Byte> bytes) {
        if (bytes == null || bytes.size() == 0) {
            return 0;
        }
        if (bytes.size() == 1) {
            return bytes.get(0).byteValue();
        }
        if (bytes.size() == 2) {
            return (65280 & (bytes.get(0).byteValue() << 8)) | (255 & bytes.get(1).byteValue());
        }
        if (bytes.size() == 3) {
            return (16711680 & (bytes.get(0).byteValue() << 16)) | (65280 & (bytes.get(1).byteValue() << 8)) | (255 & bytes.get(2).byteValue());
        }
        if (bytes.size() == 4) {
            return ((-16777216) & (bytes.get(0).byteValue() << 24)) | (16711680 & (bytes.get(1).byteValue() << 16)) | (65280 & (bytes.get(2).byteValue() << 8)) | (255 & bytes.get(3).byteValue());
        }
        return 0;
    }

    public static char byteToChar(byte b) {
        char c = (char) (0 | (255 & b));
        return c;
    }

    public static String bytesToString(List<Byte> b) {
        return ((char) (0 | (255 & b.get(0).byteValue()))) + "" + ((char) (0 | (255 & b.get(1).byteValue()))) + "" + ((char) (0 | (255 & b.get(2).byteValue()))) + "" + ((char) (0 | (255 & b.get(3).byteValue())));
    }

    public static double bytesToDouble(byte[] arr, int index) {
        long value = 0;
        int index2 = index + 7;
        for (int i = 0; i < 8; i++) {
            value |= (arr[index2 - i] & 255) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    public static long bytes2long(byte[] bs) {
        int bytes = bs.length;
        switch (bytes) {
            case 1:
                return bs[0] & 255;
            case 2:
                return ((bs[0] & 255) << 8) | (bs[1] & 255);
            case DeviceConst.SYSTEM_REBOOT /* 3 */:
                return ((bs[0] & 255) << 16) | ((bs[1] & 255) << 8) | (bs[2] & 255);
            case 4:
                return ((bs[0] & 255) << 24) | ((bs[1] & 255) << 16) | ((bs[2] & 255) << 8) | (bs[3] & 255);
            case 5:
                return ((bs[0] & 255) << 32) | ((bs[1] & 255) << 24) | ((bs[2] & 255) << 16) | ((bs[3] & 255) << 8) | (bs[4] & 255);
            case 6:
                return ((bs[0] & 255) << 40) | ((bs[1] & 255) << 32) | ((bs[2] & 255) << 24) | ((bs[3] & 255) << 16) | ((bs[4] & 255) << 8) | (bs[5] & 255);
            case 7:
                return ((bs[0] & 255) << 48) | ((bs[1] & 255) << 40) | ((bs[2] & 255) << 32) | ((bs[3] & 255) << 24) | ((bs[4] & 255) << 16) | ((bs[5] & 255) << 8) | (bs[6] & 255);
            case 8:
                return ((bs[0] & 255) << 56) | ((bs[1] & 255) << 48) | ((bs[2] & 255) << 40) | ((bs[3] & 255) << 32) | ((bs[4] & 255) << 24) | ((bs[5] & 255) << 16) | ((bs[6] & 255) << 8) | (bs[7] & 255);
            default:
                return 0L;
        }
    }

    public static String bytesToString(byte[] content, int offset, int length) {
        int effLength = checkEffectiveLength(content, offset, length);
        return new String(content, offset, effLength);
    }

    public static int checkEffectiveLength(byte[] content, int offset, int length) {
        for (int i = 0; i < length; i++) {
            try {
                int v = content[offset + i] & 255;
                if (v == 0) {
                    return i;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
        return 0;
    }

    public static byte[] hexStringToByteArray(String hexString) {
        try {
            if (!TextUtils.isEmpty(hexString)) {
                int len = hexString.length();
                byte[] bytes = new byte[len / 2];
                for (int index = 0; index < len; index += 2) {
                    String sub = hexString.substring(index, index + 2);
                    bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
                }
                return bytes;
            }
            return new byte[0];
        } catch (Exception e) {
            e.printStackTrace();
            return new byte[0];
        }
    }

    public static byte[] shortToBytes(short value) {
        byte[] temp = {(byte) (value & 255), (byte) ((value >> 8) & 65280)};
        return temp;
    }

    public static byte[] longToBytes(long num) {
        byte[] byteNum = new byte[8];
        for (int ix = 0; ix < 8; ix++) {
            int offset = 64 - ((ix + 1) * 8);
            byteNum[ix] = (byte) ((num >> offset) & 255);
        }
        return byteNum;
    }

    public static byte[] doubleToBytes(double d) {
        long value = Double.doubleToRawLongBits(d);
        byte[] byteRet = new byte[8];
        for (int i = 0; i < 8; i++) {
            byteRet[i] = (byte) ((value >> (8 * i)) & 255);
        }
        return byteRet;
    }

    public static double bytesToDouble1(byte[] arr, int index) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= (arr[index + i] & 255) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    public static void intToBytes4(int value, List<Byte> data) {
        data.set(0, Byte.valueOf((byte) value));
        data.set(1, Byte.valueOf((byte) (value >> 8)));
        data.set(2, Byte.valueOf((byte) (value >> 16)));
        data.set(3, Byte.valueOf((byte) (value >> 24)));
    }

    public static void intToBytes2(int value, List<Byte> data) {
        data.set(0, Byte.valueOf((byte) value));
        data.set(1, Byte.valueOf((byte) (value >> 8)));
    }

    public static byte[] intToBytes4(int value) {
        byte[] b = {(byte) value, (byte) (value >> 8), (byte) (value >> 16), (byte) (value >> 24)};
        return b;
    }

    public static int bytesToInt1(byte[] bytes, int index) {
        return ((-16777216) & (bytes[index + 3] << 24)) | (16711680 & (bytes[index + 2] << 16)) | (65280 & (bytes[index + 1] << 8)) | (255 & bytes[index + 0]);
    }

    public static byte[] getByteArray(float f) {
        int intbits = Float.floatToIntBits(f);
        return getByteArray(intbits);
    }

    public static void floatToBytes4(float f, List<Byte> data) {
        byte[] ret = floatToBytes4(f);
        if (ret != null && data != null && ret.length == 4 && data.size() >= 4) {
            data.set(0, Byte.valueOf(ret[0]));
            data.set(1, Byte.valueOf(ret[1]));
            data.set(2, Byte.valueOf(ret[2]));
            data.set(3, Byte.valueOf(ret[3]));
        }
    }

    public static byte[] floatToBytes4(float f) {
        int fbit = Float.floatToIntBits(f);
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (fbit >> (24 - (i * 8)));
        }
        int len = b.length;
        byte[] dest = new byte[len];
        System.arraycopy(b, 0, dest, 0, len);
        for (int i2 = 0; i2 < len / 2; i2++) {
            byte temp = dest[i2];
            dest[i2] = dest[(len - i2) - 1];
            dest[(len - i2) - 1] = temp;
        }
        return dest;
    }

    public static float bytesToFloat1(byte[] b, int index) {
        int l = b[index + 0] & 255;
        return Float.intBitsToFloat((int) ((((int) ((((int) (l | (b[index + 1] << 8))) & 65535) | (b[index + 2] << 16))) & 16777215) | (b[index + 3] << 24)));
    }

    public static int getFlagFromByte(byte data, int position) {
        int result;
        switch (position) {
            case 1:
                result = data & 1;
                break;
            case 2:
                result = data & 2;
                break;
            case DeviceConst.SYSTEM_REBOOT /* 3 */:
                result = data & 4;
                break;
            case 4:
                result = data & 8;
                break;
            case 5:
                result = data & 16;
                break;
            case 6:
                result = data & 32;
                break;
            case 7:
                result = data & 64;
                break;
            case 8:
                result = data & 128;
                break;
            default:
                result = 0;
                break;
        }
        if (result > 0) {
            return 1;
        }
        return 0;
    }

    public static void int2Byte4(int i, List<Byte> byteList) {
        if (byteList == null || byteList.size() < 4) {
            return;
        }
        byteList.set(0, Byte.valueOf((byte) ((i >> 24) & 255)));
        byteList.set(1, Byte.valueOf((byte) ((i >> 16) & 255)));
        byteList.set(2, Byte.valueOf((byte) ((i >> 8) & 255)));
        byteList.set(3, Byte.valueOf((byte) ((i >> 0) & 255)));
    }

    public static void int2Byte1(int value, List<Byte> byteList) {
        if (byteList == null || byteList.size() < 1) {
            return;
        }
        byteList.set(0, Byte.valueOf((byte) value));
    }

    public static void int2Byte2(int i, List<Byte> byteList) {
        if (byteList == null || byteList.size() < 2) {
            return;
        }
        byteList.set(0, Byte.valueOf((byte) ((i >> 8) & 255)));
        byteList.set(1, Byte.valueOf((byte) ((i >> 0) & 255)));
    }

    public static void int2Byte3(int i, List<Byte> byteList) {
        if (byteList == null || byteList.size() < 3) {
            return;
        }
        byteList.set(0, Byte.valueOf((byte) ((i >> 16) & 255)));
        byteList.set(1, Byte.valueOf((byte) ((i >> 8) & 255)));
        byteList.set(2, Byte.valueOf((byte) ((i >> 0) & 255)));
    }

    public static byte[] int2ByteArray(int i) {
        byte[] result = {(byte) ((i >> 24) & 255), (byte) ((i >> 16) & 255), (byte) ((i >> 8) & 255), (byte) (i & 255)};
        return result;
    }

    public static int byteArray2Int(byte[] bytes) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (3 - i) * 8;
            value += (bytes[i] & 255) << shift;
        }
        return value;
    }

    public static byte[] int2TwoLenByteArray(int value) {
        if (value > 32767) {
            return null;
        }
        byte[] ret = {(byte) ((value >> 8) & 255), (byte) (value & 255)};
        return ret;
    }

    public static byte[] list2byte(List<Byte> list) {
        if (list == null || list.size() < 0) {
            return null;
        }
        byte[] bytes = new byte[list.size()];
        int i = 0;
        for (Byte b : list) {
            bytes[i] = b.byteValue();
            i++;
        }
        return bytes;
    }

    public static void addByteArrayToList(List<Byte> list, byte[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            if (list instanceof ArrayList) {
                list.add(Byte.valueOf(bytes[i]));
            } else {
                list.set(i, Byte.valueOf(bytes[i]));
            }
        }
    }

    public static void addIntArrayToList(List<Byte> list, int[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            if (list instanceof ArrayList) {
                list.add(Byte.valueOf((byte) bytes[i]));
            } else {
                list.set(i, Byte.valueOf((byte) bytes[i]));
            }
        }
    }

    public static byte[] stringToByteArrayWithArrayLen(String str, int arraysLen) {
        byte[] dataToByte = str.getBytes();
        if (dataToByte.length > arraysLen) {
            return null;
        }
        byte[] ret = new byte[arraysLen];
        for (int i = 0; i < dataToByte.length; i++) {
            ret[i] = dataToByte[i];
        }
        for (int i2 = dataToByte.length; i2 < arraysLen; i2++) {
            ret[i2] = 32;
        }
        return ret;
    }

    public static void stringToFixLengthByteList(String data, List<Byte> list, int length) {
        if (data == null || data.length() < length || list == null) {
            return;
        }
        byte[] dataToByte = data.getBytes(StandardCharsets.UTF_8);
        for (int i = 0; i < length; i++) {
            list.set(i, Byte.valueOf(dataToByte[i]));
        }
    }

    public static String byteToHexStr(byte[] bytes) {
        if (bytes == null || bytes.length <= 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder("");
        for (byte b : bytes) {
            String strHex = Integer.toHexString(b & 255);
            if (strHex.length() == 1) {
                strHex = "0" + strHex;
            }
            sb.append(strHex + " ");
        }
        return sb.toString().trim();
    }

    public static String byteToHexStr(byte bytes) {
        StringBuilder sb = new StringBuilder("");
        String strHex = Integer.toHexString(bytes & 255);
        if (strHex.length() == 1) {
            strHex = "0" + strHex;
        }
        sb.append(strHex + " ");
        return sb.toString().trim();
    }

    public static String byteToHexStr3(List<Byte> bytes) {
        if (bytes == null || bytes.size() <= 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < bytes.size(); n++) {
            String strHex = "00";
            if (bytes.get(n) != null) {
                strHex = Integer.toHexString(bytes.get(n).byteValue() & 255);
            }
            if (strHex.length() == 1) {
                strHex = "0" + strHex;
            }
            sb.append(strHex + " ");
        }
        return sb.toString().trim();
    }

    public static String byteToHexStr2(byte[] bytes) {
        if (bytes == null || bytes.length <= 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder("");
        for (byte b : bytes) {
            String strHex = Integer.toHexString(b & 255);
            sb.append(strHex.length() == 1 ? "0" + strHex : strHex);
        }
        return sb.toString().trim();
    }

    public static void fix0WhenNull(List<Byte> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == null) {
                list.set(i, (byte) 0);
            }
        }
    }

    public static void test1() {
        byte[] aa = {-106, 1, 17, -111};
        DSLog.d("lyp", "a0=" + ((int) aa[0]));
        DSLog.d("lyp", "a1=" + ((int) aa[1]));
        DSLog.bytesNotSaveFile("lyp", "a=", aa);
    }
}
