c++ - Convert from QByteArray to array of double -
i have array of double:
qvector<double> y(count); i need pack qbytearray send via ethernet.
so did it. not hard:
qbytearray line; line.clear(); line.append(qbytearray::fromrawdata(reinterpret_cast<const char*>(y.data()), count*sizeof(double))); i try use code unpack data qbytearray recv :
qvector<double> data((line.size())/sizeof(double)); qbytearray dou(sizeof(double),0x0); for(int = 0; i<data.count(); i++){ dou = recv.mid(i*sizeof(double),sizeof(double)); data[i] = *reinterpret_cast<const double*>(dou.data()); dou.clear(); } but don’t it. want find out elegant way unpack qbytearray qvector<double> can me?
you can use qdatastream encode data in binary in a specific format. (more first number of items (int32) , each item)
qvector has overloads stream operators
qbytearray line; qdatastream stream(&line, qiodevice::writeonly); stream << y; and read:
qvector<double> data; qdatastream stream(line); stream >> data;
Comments
Post a Comment