c++ - How to use QDataStream::readBytes() -
according the documentation readbytes()
(in qt 5.4's qdatastream), expect following code copy input_array
newly allocated memory , point raw
@ copy:
qbytearray input_array{"\x01\x02\x03\x04qwertyuiop"}; qdatastream unmarshaller{&input_array, qiodevice::readonly}; char* raw; uint length; unmarshaller.readbytes(raw, length); qdebug() << "raw null? " << (raw == nullptr) << " ; length = " << length << endl;
...but code prints raw null? true ; length = 0
, indicating no bytes read input array.
why this? misunderstanding readbytes()
?
the documentation not describe enough, qdatastream::readbytes
expects data in format: quint32
part data length , data itself.
so read data using qdatastream::readbytes
should first write using qdatastream::writebytes
or write other way using proper format.
an example:
qbytearray raw_input = "\x01\x02\x03\x04qwertyuiop"; qbytearray ba; qdatastream writer(&ba, qiodevice::writeonly); writer.writebytes(raw_input.constdata(), raw_input.length()); qdatastream reader(ba); char* raw; uint length; reader.readbytes(raw, length); qdebug() << "raw null? " << (raw == null) << " ; length = " << length << endl;
also can use qdatastream::readrawdata
, qdatastream::writerawdata
read , write arbitrary data.
Comments
Post a Comment