QByteArray to QString

2019-03-11 16:46发布

I'm having issues with QByteArray and QString.

I'm reading a file and stores its information in a QByteArray. The file is in unicode, so it contains something like: t\0 e\0 s\0 t\0 \0 \0

I'm trying to compare this value to my specified value, but it fails, because in the debugger I see it's not an unicode string.

The code will explain everything:

QByteArray Data; //contains unicode string "t\0 e\0 s\0 t\0 \0 \0"
QString myValue = "test"; //value to compare.
if(Data.contains(myValue))
    //do some stuff.
else
    //do other stuff.

In the debugger, it shows me that the variable Data has the value "t\0 e\0 s\0 t\0 \0 \0" and myValue has the value "test". How can I fix it?

8条回答
不美不萌又怎样
2楼-- · 2019-03-11 17:45

One may find QString::fromUtf8() also useful.

For QByteArray input of "\010" and "\000", QString::fromLocal8Bit(input, 1) returned "\010" and "", but QString::fromUtf8(input, 1) correctly returned "\010" and "\000".

查看更多
狗以群分
3楼-- · 2019-03-11 17:45

You can use:

QString DataAsString = data.trimmed();

Data is a QByteArray.

查看更多
登录 后发表回答