QByteArray 与 QString 之间的转换
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QImage image("test.png");
QByteArray ba;
QBuffer buf(&ba);
image.save(&buf, "BMP");
QByteArray compressed = qCompress(ba, 1); // better just open file with QFile, load data, compress and toHex?
QByteArray hexed = compressed.toHex();
// save to a file
QString str(hexed);
QFile f("test.hex");
if (f.exists())
f.remove();
if (f.open(QFile::WriteOnly))
{
f.write(str.toLatin1()); // holds only 0..f nothing special.
}
else
qDebug("failed to open file \"test.hex\"");
f.close();
////----------
if (f.open(QFile::ReadOnly))
{
QByteArray read = f.readAll();
f.close();
QString rStr = QString::fromLatin1(read.data(), read.size());
if (rStr != str)
qDebug("Writed and read two different hexed strings.");
QByteArray readCompressed = QByteArray::fromHex(rStr.toAscii());
if (readCompressed != compressed)
qDebug("bytes before hexing and dehexing _is_ different.");
QByteArray readDecompressed = qUncompress(readCompressed);
if (readDecompressed != ba)
qDebug("bytes before and after compressions are different.");
QImage readImg;
//QBuffer readBuf(&readDecompressed);
readImg.loadFromData(readDecompressed);
if (readImg.isNull())
qDebug("The image is null. Something failed.");
readImg.save("test.bmp");
}
else
qDebug("failed to open test.hex file for reading");
return 0;
}
阅读(3529) | 评论(0) | 转发(0) |