使用 Qt 将二进制 std::stringstream 内容转换为 base64(并返回)

convert a binary std::stringstream content to base64 (and back) with Qt

我想在 json 文件中保存一些犰狳矩阵,一旦进行 base64 编码。 这是我正在工作的一个不完整的代码示例。 我的问题是从 std::stringstream 转换为 QByteArray 并在不丢失我的 stringstream 原始(准确)内容的情况下返回。

arma::file_type arma_save_format=arma::arma_binary;
std::stringstream savemedium;

//init & save
{
    std::stringstream ss1;
    arma::mat m;
    m<<1<<2<<3<<arma::endr<<4<<5<<6<<arma::endr<<7<<8<<9;
    std::cout<<"#1 m:"<<m<<std::endl;
    m.save(ss1, arma_save_format);
    savemedium<<ss1.str();
}

/*************************/
std::cout<<"inter:"<<savemedium.str()<<std::endl<<std::endl;
QByteArray ba1;

//here I'd like to convert savemedium to a base64 QString, probably using a qbyteArray,
//and convert it back to std::stringstream savemedium


savemedium.str("");
savemedium<< /* ?? */;

std::cout<<"inter:"<<savemedium.str()<<std::endl<<std::endl;
/*************************/
//restitution
{
    std::stringstream ss2;
    ss2<<savemedium.str();
    arma::mat m2;
    m2.load(ss2, arma_save_format);
    std::cout<<"#2:"<<m2<<std::endl;

}

这是我的工作方式:

#include <armadillo>
#include <iostream>
#include <QByteArray>
#include <sstream>


// to print the content of a QByteArray
void print(const QByteArray &ba) {
    QByteArray::const_iterator it=ba.begin();
    uint i=0;
    while (it!=ba.end()) {
        std::cout<<ba.at(i);
        i++;
        it++;
    }
}

int main(int, char **) {
arma::file_type arma_save_format=arma::arma_binary;
std::stringstream savemedium;

//init & save
{
    std::stringstream ss1;
    arma::mat m;
    m<<1<<2<<3<<arma::endr<<4<<5<<6<<arma::endr<<7<<8<<9;
    std::cout<<"#1 m:"<<m<<std::endl;
    m.save(ss1, arma_save_format);
    savemedium<<ss1.str();
}


/*************************/
std::cout<<"savemedium:"<<std::endl<<savemedium.str()<<std::endl<<std::endl;

//  Here I'd like to convert savemedium to a base64 QString, probably using a qbyteArray,
//  and convert it back to std::stringstream savemedium
//  the following block commented, savemedium is left unmodified and I get the matrix back.
{
    QByteArray ba1;
    ba1.setRawData(savemedium.str().c_str(), savemedium.str().size());

    print(ba1);
    std::cout<<std::endl<<"ba1 count:"<<ba1.count()<<std::endl<<std::endl;

    QByteArray b64(ba1.toBase64());

    std::cout<<"b64:"<<std::endl;
    print(b64);
    std::cout<<std::endl<<std::endl;

    QByteArray ba2(QByteArray::fromBase64(b64));
    std::cout<<"ba2:"<<std::endl;
    print(ba2);
    std::cout<<std::endl<<"ba2 count:"<<ba2.count()<<std::endl<<std::endl;


    std::cout<<"What we get if we just use QByteArray::data()"<<std::endl<<"ba2:"<<std::endl<<ba2.data()<<std::endl;

    savemedium.str(""); //emptying savemedium so we fill it with decoded data

    QByteArray & ba=ba2;    //just a reference for typing comfort

    QByteArray::const_iterator it=ba.begin();
    uint i=0;
    while (it!=ba.end()) {
        savemedium<<ba.at(i);
        i++;
        it++;
    }
}
std::cout<<"savemedium:"<<std::endl<<savemedium.str()<<std::endl<<std::endl;
/*************************/
//restitution
{
    std::stringstream ss2;
    ss2<<savemedium.str();
    arma::mat m2;
    m2.load(ss2, arma_save_format);
    std::cout<<"#2:"<<m2<<std::endl;

}

return 0;
}

这给出了以下输出:

#1 m:   1.0000   2.0000   3.0000
   4.0000   5.0000   6.0000
   7.0000   8.0000   9.0000

savemedium:
ARMA_MAT_BIN_FN008
3 3
�?@@@@ @@"@

ARMA_MAT_BIN_FN008
3 3
�?@@@@ @@"@
ba1 count:95

b64:
QVJNQV9NQVRfQklOX0ZOMDA4CjMgMwoAAAAAAADwPwAAAAAAABBAAAAAAAAAHEAAAAAAAAAAQAAAAAAAABRAAAAAAAAAIEAAAAAAAAAIQAAAAAAAABhAAAAAAAAAIkA=

ba2:
ARMA_MAT_BIN_FN008
3 3
�?@@@@ @@"@
ba2 count:95

What we get if we just use QByteArray::data()
ba2:
ARMA_MAT_BIN_FN008
3 3

savemedium:
ARMA_MAT_BIN_FN008
3 3
�?@@@@ @@"@

#2:   1.0000   2.0000   3.0000
   4.0000   5.0000   6.0000
   7.0000   8.0000   9.0000

Press  to close this window...

我使用 QByteArray 因为它有 base64 编码方法,std::stringstream 因为犰狳 mat::load() 或保存句柄流。

QByteArray 无法使用 << 或 append() 安全地提供二进制数据,这会在遇到一些特殊字符时截断数据(我想是“\0”)。

这就是使用的原因:

ba1.setRawData(savemedium.str().c_str(), savemedium.str().size());

一旦数据在 QByteArray 中,检索它就不是一件容易的事 把它放在一个字符串流中: 我是这样做的:

  QByteArray::const_iterator it=ba.begin();
    uint i=0;
    while (it!=ba.end()) {
        savemedium<<ba.at(i);
        i++;
        it++;
    }

欢迎任何更好的解决方案。使用 QByteArray::data() 也会截断二进制数据,原因可能相同。 感谢您的回答和评论。