Convert QByteArray to QString - c++

I want to encrypt the data of a database and to do this, I used AES_128 in this link for encryption.
The result of encryption is a QByteArray and the QByteArray is saved on the text file in the correct shape and I could decode it correctly, but and I need to convert it to the QString and reverse to QByteArray to store and read it on the Sqlite DB. I tried some options like
QByteArray encodedText; QString DataAsString = QString(encodedText);
and
string DataAsString1 = encodedText.toStdString();
and
QString DataAsString = QTextCodec::codecForName("UTF-8") >toUnicode(encodedText);
and other solutions like this link, but the outputs of these options aren't in the correct way. Because after casting, I couldn't convert the encoded text to decoded correctly.
This is the input string of encoded text:
"\x14r\xF7""6#\xFE\xDB\xF0""D\x1B\xB5\x10\xEDx\xE1""F"
and these are the outputs for the different options:
\024r�6#���D\033�\020�x�F
and
\024r�6#���D\033�\020�x�F
Does anybody suggestion about the right conversion?

try to use this:
QString QString::fromUtf8(const QByteArray &str)

Related

Image loading error from base64 string Qt

string text = "Some base64 string";
QByteArray bytes = QByteArray::fromBase64(text.c_str());
cover.loadFromData(bytes);
This is my code. It's work fine, when base64 string starts with "iV". But in another case (for example when it's start with "/9j/") cover returns NULL. What's the problem?

QUrlQuery append?

Is it possible to use QUrlQuery to append data without striping the url?
Using the code bellow will strip everything after the "?" and
the result is:
https://foobar.com/Info.xml.aspx?userdata=1234
I would like to get:
https://foobar.com/Info.xml.aspx?user=jack&userdata=1234
QUrl url("https://foobar.com/Info.xml.aspx?user=jack&");
QString data = "1234";
QUrlQuery query;
query.addQueryItem("userdata", data);
url.setQuery(query);
I'm asking because i need to make multiple calls, every time adding a new parameter and "building" the url from scratch every time is annoying.
You have to get the query and then add the item:
QUrl url("https://foobar.com/Info.xml.aspx?user=jack&");
QString data = "1234";
QUrlQuery query(url.query());
query.addQueryItem("userdata", data);
url.setQuery(query);
qDebug()<<url;
Output:
QUrl("https://foobar.com/Info.xml.aspx?user=jack&userdata=1234")

QString& QString::operator=(const QByteArray&)' is private

I am trying to read standard output from QProcess as QString where the passed argument is a linux command. The linux command gives me the linux username. When I pass the argument to QProcess I expect the output to be my linux username. In doing so I have to read the standard output and get the result as QString but I get the error:
QString& QString::operator=(const QByteArray&)' is private.
My code:
QProcess process;
process.start(QString::fromStdString("whoami"));
process.waitForFinished(-1); // will wait forever until finished
QByteArray name = process.readAllStandardOutput();
QString username = name; //Error here saying
QProcess process;
process.start(QString::fromStdString("whoami"));
process.waitForFinished(-1); // this could be omitted
QTextStream txtStream(&process);
QString username = txtStream.readLine();
Note QTextStream by default is using default locale string encoding what is preferred. You can use QTextStream::setCodec to change string encoding (UTF-8, Windows-1250, UCS or whatever you need, default codec from system locale is usually best choice).
It also allows you to process data in streamed manner and this is always good.
Simply do this:
QByteArray name = process.readAllStandardOutput();
QString username = QString::fromRawData(name.data(), name.size());

Load and display QString with proper encoding

I am trying to load a name from file that has several special characters and if it is in file (looks like meno: Marek Ružička/) display it. Code here:
QFile File("info/"+meno+".txt");
File.open(QIODevice::ReadOnly);
QVariant Data(File.readAll());
QString in = Data.toString(), pom;
if(in.contains("meno:")){
pom = in.split("meno:").at(1);
pom=pom.split("/").at(0);
ui->label_meno->setText(trUtf8("Celé meno: ")+pom);}
the part trUtf8("Celé meno: ") displays well but I cant find how to display string in pom, it alone looks like Marek RužiÄka, using toUtf8() function makes it Marek RuþiÃÂka, I've tried to convert it to stdString too but doesn't work either. I am not sure if the conversion from QFile to QVariant and to QString is right, if this causes problem how to read data properly?
Try this:
QTextCodec* utf = QTextCodec::codecForName("UTF-8");
QByteArray data = <<INPUT QBYTEARRAY>>.toUtf8();
QString utfString = utf->toUnicode(data);
qDebug() << utfString;
One of the right ways is to use QTextStream for the reading, and then you can specify the codec for utf 8 as follow:
in.setCodec("UTF-8");
See the documentation for further details:
void QTextStream::setCodec(const char * codecName)
Sets the codec for this stream to the QTextCodec for the encoding specified by codecName. Common values for codecName include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.
Example:
QTextStream out(&file);
out.setCodec("UTF-8");
Another right way would be to fix your current code without using QTextStream by using the dedicated QString method as follows:
QString in = QString::fromUtf8(File.readAll()), pom;
Please note that though you may wish to add more error handling into your code than available now.

Detect text file encoding

In my program I load plain text files supplied by the user:
QFile file(fileName);
file.open(QIODevice::ReadOnly);
QTextStream stream(&file);
const QString &text = stream.readAll();
This works fine when the files are UTF-8 encoded, but some users try to import Windows-1252 encoded files, and if they have words with special characters (for example "è" in "boutonnière"), those will show incorrectly.
Is there a way to detect the encoding, or at least distinguish between UTF-8 (possibly without BOM), and Windows-1252, without asking the user to tell me the encoding?
Turns out that auto-detecting the encoding is impossible for the general case.
However, there is a workaround to at least fall back to the system locale if the text is not valid UTF-8/UTF-16/UTF-32 text. It uses QTextCodec::codecForUtfText(), which tries to decode a byte array using UTF-8, UTF-16 and UTF-32, and returns the supplied default codec if it fails.
Code to do it:
QTextCodec *codec = QTextCodec::codecForUtfText(byteArray, QTextCodec::codecForName("System"));
const QString &text = codec->toUnicode(byteArray);
Update
The above code will not detect UTF-8 without BOM, however, as codecForUtfText() relies on the BOM markers. To detect UTF-8 without BOM, see https://stackoverflow.com/a/18228382/492336.
This trick works for me, at least so far. This method does not require BOM to work:
QTextCodec::ConverterState state;
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
const QByteArray data(readSource());
const QString text = codec->toUnicode(data.constData(), data.size(), &state);
if (state.invalidChars > 0)
{
// Not a UTF-8 text - using system default locale
QTextCodec * codec = QTextCodec::codecForLocale();
if (!codec)
return;
ui->textBrowser->setPlainText(codec->toUnicode(readSource()));
}
else
{
ui->textBrowser->setPlainText(text);
}