Qt C++ QString 到 QByteArray 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37802575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:43:16  来源:igfitidea点击:

Qt C++ QString to QByteArray Conversion

c++qtqstringqbytearray

提问by James

I have created an encrypt/decrypt program, when encrypting I store the encrypted QByteArray in a text file.

我创建了一个加密/解密程序,加密时我将加密的 QByteArray 存储在一个文本文件中。

When trying to decrypt I retrieved it and then put it into the decryption method, the problem is that I need a way to convert it to QByteArray without changing the format, otherwise it will not decrypt properly. What I mean is if the file gave me an encrypted value of 1234 and I converted that to QByteArray by going 1234.toLatin1()it changes the value and the decryption does not work. Any suggestions?

尝试解密时,我检索了它,然后将其放入解密方法中,问题是我需要一种方法将其转换为 QByteArray而不更改格式,否则将无法正确解密。我的意思是,如果该文件给了我一个 1234 的加密值,我将1234.toLatin1()它转换为 QByteArray,它会更改值并且解密不起作用。有什么建议?

My Code:

我的代码:

QFile file(filename);
    QString encrypted;
    QString content;

    if (file.open(QIODevice::ReadOnly)) {
        QTextStream stream( &file );
        content = stream.readAll();
    }

    encrypted = content.replace("\n", "");

    qDebug() << encrypted; // Returns correct encrypted value

    QByteArray a;
    a += encrypted;

    qDebug() << "2 " + a; // Returns different value than previous qDebug()

    QByteArray decrypted = crypto.Decrypt(a, key);
    return decrypted;

回答by Daniel

I guess you should use:

我想你应该使用:

QString::fromUtf8(const QByteArray &str)

Or:

或者:

QString::QString(const QByteArray &ba)

to convert QByteArray to QString, then write it into file by QTextStream.
After that, read file by QTextStream, use:

将 QByteArray 转换为 QString,然后通过 QTextStream 将其写入文件。
之后,通过 QTextStream 读取文件,使用:

QString::toUtf8()

to convert QString to QByteArray.

将 QString 转换为 QByteArray。

QString::QString(const QByteArray &ba)

Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromUtf8().

QString::QString(const QByteArray &ba)

构造一个用字节数组 ba 初始化的字符串。使用 fromUtf8()将给定的字节数组转换为 Unicode 。


P.S: Maybe use QFile::write and QFile::read is a better way.


PS:也许使用 QFile::write 和 QFile::read 是更好的方法。

回答by Ahmed Yossef

try using toLocal8Bit() .. it works fine with me

尝试使用 toLocal8Bit() .. 它对我来说很好用

回答by Skonitsa

Or simply go with b64 = data.toUtf8().toBase64();

或者干脆一起去 b64 = data.toUtf8().toBase64();

First convert it to QByteArraywith the toUtf8()and then immediately convert it to toBase64()

首先将其转换为QByteArraytoUtf8()然后立即将其转换为toBase64()

回答by Ritoban Roy Chowdhury

If I understand correctly, the text from the file is store in the QString content. I think you could create a new QByteArray. Because the constructor of a QByteArray does not allow a QString as input, I will probably have to append the QString to the empty QByteArray.

如果我理解正确,文件中的文本存储在 QString 内容中。我认为您可以创建一个新的 QByteArray。因为 QByteArray 的构造函数不允许 QString 作为输入,我可能不得不将 QString 附加到空的 QByteArray。

//After if:  
QByteArray tempContent();
tempContent.append(content);
QByteArray decrypted = crypto.Decrypt(tempContent, key);

I do not have much experience in the Qt library, but I hope this helps.

我在 Qt 库方面没有太多经验,但我希望这会有所帮助。