#ifndef QBYTEUTIL_H
#define QBYTEUTIL_H

#include <QObject>

class QByteUtil : public QObject
{
    Q_OBJECT
public:
    explicit QByteUtil(QObject *parent = nullptr);


    /******** 字节数组与字符串互转 ********/
    /**
     * @brief binToHexString
     * @param bytes
     * @note 16进制字节数组转字符串，用于输出显示，不含空格
     * @return
     */
    static QString binToHexString(QByteArray bytes);
    /**
     * @brief hexStringToBytes
     * @param hexString
     * @note 16进制字符串转字节数组，不含空格
     * @return
     */
    static QByteArray hexStringToBytes(QString hexString);

    /******** 字节数组与long互转 ********/
    /**
     * @brief binToULong
     * @param bytes
     * @note 16进制字节数组转无符号long，length个字节。字符串顺序，高位在前，低位在后
     * 如0x0080000000086A43 = 36028797019515459
     * @return
     */
    static qulonglong binToULong(QByteArray bytes, quint8 length);
    static QByteArray ULongToBytes(qulonglong value);

    /******** 字节数组与float互转 ********/
    /**
     * @brief binToFloat
     * @param bytes
     * @note 4个字节转float浮点数，从左到右排序
     * @example {0x42, 0xF6, 0xE6, 0x66} 转换为 123.45
     * @return
     */
    static float binToFloat(QByteArray bytes);
    /**
     * @brief binToFloatArray
     * @param bytes
     * @note 字节数组转float数组，16进制浮点数，4个字节表示1个float浮点数，从左到右排序
     * @example {0xBD, 0x1F, 0xB1, 0xDA, 0x40, 0x63, 0x02, 0x0C, 0x40, 0x49, 0x0F, 0xDA} 转换为 -0.038988, 3.547, 3.141593
     * @return
     */
    static QVector<float> binToFloatArray(QByteArray bytes);
    /**
     * @brief floatToBytes
     * @param value
     * @note 单个float数转4字节数组，从左至右排序
     * @example 123.45 转换为 {0x42, 0xF6, 0xE6, 0x66}
     * @return
     */
    static QByteArray floatToBytes(float value);
    /**
     * @brief floatArrayToBytes
     * @param floatArray
     * @note float数组转换为字节数组，每一个float浮点数4字节，从左到右排序
     * @example 0.0608, 2.28884, 0.00679329 转换为 {0x3D, 0x79, 0x09, 0x6C, 0x40, 0x12, 0x7C, 0x5B, 0x3B, 0xDE, 0x9A, 0x3F}
     * @return
     */
    static QByteArray floatArrayToBytes(QVector<float> floatArray);

    /******** 字节数组与double互转 ********/
    /**
     * @brief binToDouble
     * @param bytes
     * @note 8个字节转double双精度浮点数，从左到右排序
     * @example C00921FB53D92715 转换为 -3.141592650475
     * @return
     */
    static double binToDouble(QByteArray bytes);
    /**
     * @brief binToDoubleArray
     * @param bytes
     * @note 字节数组转double数组，16进制双精度浮点数，8个字节表示1个double浮点数，从左到右排序
     * @example BFA3F63C31DF761D400C604189374BC74039B2DE00D1B717 转换为 -0.038988, 3.547, 3.141593
     * @return
     */
    static QVector<double> binToDoubleArray(QByteArray bytes);
    /**
     * @brief doubleToBytes
     * @param value
     * @note 单个double数转换为8字节数组，从左到右排序
     * @example 123.45 转换为 405EDCCCCCCCCCCD
     * @return
     */
    static QByteArray doubleToBytes(double value);
    /**
     * @brief doubleArrayToBytes
     * @param doubleArray
     * @note double数组转换为字节数组，每个double双精度浮点数8字节，从左到右排序
     * @example 0.0608, 2.28884, 0.00679329 转换为 3FAF212D77318FC540024F8B588E368F3F7BD347E61DABB7
     * @return
     */
    static QByteArray doubleArrayToBytes(QVector<double> doubleArray);

private:
    static QByteArray appendZeroAlign(QByteArray array, int count);

signals:

};

#endif // QBYTEUTIL_H
