#include "QByteUtil.h"
#include <QVector>

static uchar uc = 0x00;
const int FLOAT_BYTE_LENGTH = 4;
const int DOUBLE_BYTE_LENGTH = 8;

QByteUtil::QByteUtil(QObject *parent) : QObject(parent)
{

}

QByteArray QByteUtil::appendZeroAlign(QByteArray array, int count)
{
    // 如果字节数组的长度不足cout的倍数，在字节数组的前段补0x00
    int rem = array.length() % count;
    if (rem > 0)
    {
        array.insert(0, count - rem, uc);
    }

    return array;
}

QString QByteUtil::binToHexString(QByteArray bytes)
{
    return bytes.toHex().toUpper();
}

QByteArray QByteUtil::hexStringToBytes(QString hexString)
{
    hexString = hexString.toUpper();
    if (hexString.length() % 2 == 1)
    {
        hexString = "0" + hexString;
    }

    bool ok;
    QByteArray bytes;
    for (int i = 0; i < hexString.length() - 1; i = i + 2)
    {
        QString str = hexString.mid(i, 2);
        bytes.append(str.toInt(&ok, 16));
    }

    return bytes;
}

qulonglong QByteUtil::binToULong(QByteArray bytes, quint8 length)
{
    qulonglong value = 0;

    for (int i = 0; i < bytes.length() && i < length; i++)
    {
        value = value * 256 + (quint8) bytes.at(i);
    }

    return value;
}


float QByteUtil::binToFloat(QByteArray bytes)
{
    bytes = appendZeroAlign(bytes, FLOAT_BYTE_LENGTH);

    // 如果字节数组长度超过4位，取前4位
    QString str = QByteUtil::binToHexString(bytes.mid(0, FLOAT_BYTE_LENGTH));
    int hex = str.toUInt(0, 16);
    float ret = *(float*) &hex;

    return ret;
}

QVector<float> QByteUtil::binToFloatArray(QByteArray bytes)
{
    bytes = appendZeroAlign(bytes, FLOAT_BYTE_LENGTH);

    // float用4字节浮点数表示
    int length = bytes.length() / FLOAT_BYTE_LENGTH;

    // 返回值
    QVector<float> ret;

    // 4个字节的浮点数，转换为
    for (int i = 0; i < length; i++)
    {
        QString str = QByteUtil::binToHexString(bytes.mid(i * FLOAT_BYTE_LENGTH, FLOAT_BYTE_LENGTH));

        int hex = str.toUInt(0, 16);
        float fl = *(float*) &hex;

        ret.append(fl);
    }

    return ret;
}

QByteArray QByteUtil::floatToBytes(float value)
{
    uchar * hex = (uchar *) &value;
    QByteArray ret;
    for (int i = 0; i < FLOAT_BYTE_LENGTH; i++)
    {
        ret.insert(0, hex[i]);
    }

    return ret;
}

QByteArray QByteUtil::floatArrayToBytes(QVector<float> floatArray)
{
    QByteArray ret;
    for (int i = 0; i < floatArray.length(); i++)
    {
        ret.append(floatToBytes(floatArray.at(i)));
    }
    return ret;
}


double QByteUtil::binToDouble(QByteArray bytes)
{
    bytes = appendZeroAlign(bytes, DOUBLE_BYTE_LENGTH);

    // 如果字节数组长度超过8位，取前8位
    QString str = QByteUtil::binToHexString(bytes.mid(0, DOUBLE_BYTE_LENGTH));
    qulonglong hex = str.toULongLong(0, 16);
    double ret = *(double*) &hex;

    return ret;
}

QVector<double> QByteUtil::binToDoubleArray(QByteArray bytes)
{
    bytes = appendZeroAlign(bytes, DOUBLE_BYTE_LENGTH);

    // double用8字节浮点数表示
    int length = bytes.length() / DOUBLE_BYTE_LENGTH;

    // 返回值
    QVector<double> ret;

    // 8个字节的双精度浮点数，转换为
    for (int i = 0; i < length; i++)
    {
        QString str = QByteUtil::binToHexString(bytes.mid(i * DOUBLE_BYTE_LENGTH, DOUBLE_BYTE_LENGTH));

        qulonglong hex = str.toULongLong(0, 16);
        double dl = *(double*) &hex;

        ret.append(dl);
    }

    return ret;
}

QByteArray QByteUtil::doubleToBytes(double value)
{
    uchar * hex = (uchar *) &value;
    QByteArray ret;
    for (int i = 0; i < DOUBLE_BYTE_LENGTH; i++)
    {
        ret.insert(0, hex[i]);
    }

    return ret;
}

QByteArray QByteUtil::doubleArrayToBytes(QVector<double> doubleArray)
{
    QByteArray ret;
    for (int i = 0; i < doubleArray.length(); i++)
    {
        ret.append(doubleToBytes(doubleArray.at(i)));
    }
    return ret;
}
