C++ 无损转换为 std::string 和 QByteArray 的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10767760/
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-27 14:26:04  来源:igfitidea点击:

Correct way to losslessly convert to and from std::string and QByteArray

c++stringqtbinary

提问by Jake Petroules

What is the correct way to convert losslessly between std::string and QByteArray... mostly for the purpose of handling binary data?

在 std::string 和 QByteArray 之间无损转换的正确方法是什么......主要是为了处理二进制数据?

I'm using:

我正在使用:

QByteArray qba = QString::fromStdString(stdString).toAscii();

and

QString(qba).toStdString();

but I wanted to check whether this is actually correct.

但我想检查这是否真的正确。

回答by leemes

For binary data your solution is problematic since non-ASCII characters would be converted to '?'by QString::toAscii(). There is also the unnecessary overhead of UTF-16 conversion for the internal representation of QString. As you might have guessed, QStringshould only be used if the data is textual, not binary.

对于二进制数据的解决方案是有问题的,因为非ASCII字符将被转换为'?'通过QString::toAscii()。对于QString. 正如您可能已经猜到的那样,QString仅当数据是文本数据而不是二进制数据时才应使用。

Both QByteArrayand std::stringhave constructors for raw data (C-string + length) and also a conversion to C-string + length. So you can use them for conversion:

二者QByteArraystd::string具有用于原始数据(C-串+长度),也转换到C-串+长度构造函数。所以你可以使用它们进行转换:

// std::string => QByteArray
QByteArray byteArray(stdString.c_str(), stdString.length());

// QByteArray => std::string
std::string stdString(byteArray.constData(), byteArray.length());

They are both binary-safe, meaning that the string may contain '\0'characters and doesn't get truncated. The data also doesn't get touched (there is no UTF conversion), so this conversion is "lossless".

它们都是二进制安全的,这意味着字符串可能包含'\0'字符并且不会被截断。数据也没有被触及(没有 UTF 转换),所以这种转换是“无损的”。

Make sure to use the constructors with the length as the second argument (for both QByteArrayand std::string), as most other constructors will truncate the data before the first occurrence of a zero.

确保使用将长度作为第二个参数的构造函数(对于QByteArraystd::string),因为大多数其他构造函数会在第一次出现零之前截断数据。

回答by Parker Coates

As of Qt 5.4, QByteArrayhas built in support for std::stringconversions via QByteArray::toStdString()and QByteArray::fromStdString().

从 Qt 5.4 开始,QByteArray已经内置了对std::string通过QByteArray::toStdString()和进行转换的支持QByteArray::fromStdString()

These are implemented almost identically to leemes' answer.

这些几乎与leemes 的答案相同。

inline std::string QByteArray::toStdString() const
{ return std::string(constData(), length()); }

inline QByteArray QByteArray::fromStdString(const std::string &s)
{ return QByteArray(s.data(), int(s.size())); }

回答by Veysel Bozdo?ano?lu

QByteArray datagram; datagram.append(ui->lineEdit_sendData->text());

QByteArray 数据报;datagram.append(ui->lineEdit_sendData->text());