Qbytearray 与 float , int 等互转

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QByteArray buff;
    float ff = 1.234f, f2;
    int ii = 345,i2;


//    int len = sizeof(int) + sizeof(float);
//    buff.resize(8);
//    qDebug()<< "size=" << buff.size();
    buff.append(reinterpret_cast<const char*>(&ii),sizeof(ii));
    buff.append(reinterpret_cast<const char*>(&ff),sizeof(ff));
//    i2 = *reinterpret_cast<const int*>(buff.data());
//    f2 = *reinterpret_cast<const float*>(buff.data()+sizeof(int));
//    qDebug() << len << " " <<  buff.size() << " " <<  ii << " " << i2 << " " << ff <<" " << f2;
    i2 = *reinterpret_cast<const int*>(buff.data());
    f2 = *reinterpret_cast<const float*>(buff.data()+sizeof(ii));
    qDebug() <<  buff.size() << " " <<  ii << " " << i2 << " " << ff <<" " << f2;

    return a.exec();
}

posted on 2023-01-20 21:24  xihong  阅读(290)  评论(0编辑  收藏  举报

导航