how to convert unicode to printable string in QT stream - qt

I'm writing a stream to a file and stdout, but I'm getting some kind of encoding like this:
\u05ea\u05e7\u05dc\u05d9\u05d8
\u05e9\u05e1\u05d9\u05de\u05dc
\u05e9\u05d9\u05e0\u05d5\u05d9
\u05d1\u05e1\u05d2\u05e0\u05d5\u05df
\u05dc\u05d3\u05e2\u05ea\u05d9
\u05d0\u05dd \u05d0\u05e0\u05d9
\u05d6\u05d5\u05db\u05e8
\u05e0\u05db\u05d5\u05df
How can I convert this to a printable string?

I can't figure out how you are printing the string, but that is just Unicode:
#include <QString>
#include <QFile>
#include <QDebug>
int main(int argc, char **argv)
{
QString s = "\u05ea\u05e7\u05dc\u05d9\u05d8 \u05e9\u05e1\u05d9\u05de\u05dc \u05e9\u05d9\u05e0\u05d5\u05d9 \u05d1\u05e1\u05d2\u05e0\u05d5\u05df \u05dc\u05d3\u05e2\u05ea\u05d9 \u05d0\u05dd \u05d0\u05e0\u05d9 \u05d6\u05d5\u05db\u05e8 \u05e0\u05db\u05d5\u05df";
QFile file1("1.txt");
if (!file1.open(QIODevice::WriteOnly | QIODevice::Text))
return 1;
QTextStream out(&file1);
out << s << "\n";
qDebug() << s;
return 0;
}
If I compile and run it
g++ -lQtCore -I /usr/include/QtCore test.cpp
./a.out
I can see the printable characters both in the console debug output and in the file:
"תקליט שסימל שינוי בסגנון לדעתי אם אני זוכר נכון"
So you are probably doing something wrong or looking in the wrong direction, can you paste your code so we can help you better?

Related

Recommended Poco DatagramSocket Receive example doesn't compile

Using Poco-1.12.4-release and g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, The recommended example code for DatagramSocket Receive in this doc:
https://pocoproject.org/slides/200-Network.pdf
compiles with this error:
dgs.cpp: In function ‘int main(int, char**)’:
dgs.cpp:34:33: error: no matching function for call to ‘Poco::Net::DatagramSocket::DatagramSocket(Poco::Net::SocketAddress&)’
34 | Poco::Net::DatagramSocket dgs(sa);
What happened? Is there a fix for this example?
Thanks
The code is:
// DatagramSocket receive example
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
#include <iostream>
int main(int argc, char** argv)
{
Poco::Net::SocketAddress sa(Poco::Net::IPAddress(), 514);
Poco::Net::DatagramSocket dgs(sa);
char buffer[1024];
for (;;)
{
Poco::Net::SocketAddress sender;
int n = dgs.receiveFrom(buffer, sizeof(buffer)-1, sender);
buffer[n] = '\0';
std::cout << sender.toString() << ": " << buffer << std::endl;
}
return 0;
}
I tried this:
Poco::Net::SocketAddress sa(Poco::Net::IPAddress(), 514);
Poco::Net::DatagramSocket dgs(Poco::Net::SocketAddress::IPv4);
dgs.connect(sa);
It compiles, but is it correct?

QString::toLocal8Bit() on Windows

I have a case which can be summarized like the code below:
#include <QCoreApplication>
#include <QStringList>
#include <QDir>
#include <QCryptographicHash>
#include <iostream>
#include <QDebug>
#include <QObject>
using namespace std;
QString Encode(const QString & filePath)
{
auto temp = filePath.toLocal8Bit();
qDebug() << QString("Encode 8 bit: ") << temp;
return temp.toPercentEncoding(" ");
}
QString Decode(const QString & filePath)
{
auto temp = filePath.toLocal8Bit();
qDebug() << QString("Decode 8 bit: ") << temp;
return QByteArray::fromPercentEncoding(temp);
}
int main()
{
QString raw = Decode(QString("K%C3%A4ssbohrer"));
qDebug() << QString("Raw:") << raw;
QString encoded = Encode(raw);
}
And this code outputs:
"Decode 8 bit: " "K%C3%A4ssbohrer"
"Raw:" "Kõssbohrer"
"Encode 8 bit: " "K\xE4ssbohrer"
As can be seen when I call toLocal8Bit() on a string containing unicode is ending up different with what I initially have. And this problem only happens in windows.
What should I do for making my Encode and Decode functions work mutually
both in windows and linux? I mean with Encode I want to return a QString which I can use Decode later to obtain the original QString which may contain unicode.

QFileDialog created without file extension in linux

I am creating a file in both windows and linux using QFileDialog
fileName = QFileDialog::getSaveFileName(this, tr("Create project"), applicationPath,tr("Files (*.MSC)"));
In windows the file is created as path/to/file.MSC
but in linux file is created as path/to/file
why .MSC is not appending in Linux, whether we need to use other function for this
The following example works fine on Linux. You get the file myfile.MSC with the text "test" written on it.
#include <iostream>
#include <QApplication>
#include <QFileDialog>
#include <QString>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString applicationPath = QDir::currentPath() + "/myfile.MSC";
QString fileName = QFileDialog::getSaveFileName(0,
QApplication::tr("Create project"),
applicationPath,
QApplication::tr("Files (*.MSC)"));
if (fileName.isEmpty())
return -1;
else {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
std::cout << "error\n";
return -1;
}
QDataStream out(&file);
out << "test";
}
return a.exec();
}

Getting different values from std::stod() and QString::toDouble() in Qt5

I started a Qt project and linked to it some C++ code I wrote to parse a file. This code is using std::stod() to parse double values, and works fine in a plain c++ project, but when used with a Qt application, std::stod() returns only the integer part of the number.
I wrote and ran some test code, one compiled with g++ 6.1, and the other with qmake 5.6 and the same g++. The results are the same as my projects results.
code compiled with g++ :
#include <iostream>
int main(int argc, char ** argv)
{
const std::string number("3.14");
double dbl = std::stod(number);
std::cout << dbl << '\n'; // 3.14
return 0;
}
It shows the good value : 3.14
code compiled with Qt :
#include <QCoreApplication>
#include <QDebug>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
const std::string snumber("3.14");
const QString qnumber = QString::fromStdString("3.14");
double std_d = std::stod(snumber);
double qt_d = qnumber.toDouble();
qDebug() << std_d << qt_d; // 3 3.14
std::cout << std_d << ' ' << qt_d << '\n'; // 3 3.14
return a.exec();
}
Can you tell me why std::stod() behaves like this ?
Because std::stod is broken beyond repair and should burn in hell is implemented in terms of std::strtod, which interprets doubles according to the current locale. QString instead always uses the C locale (use QLocale for locale-specific conversions).
Creating the QCoreApplication instance causes a call to setlocale(LC_ALL, ""), which sets the process' locale according to the environment, thus changing std::stod behavior.

Qt is successfully creating a file but not writing to it with QTextStream

Hey I'm trying to mess around with Qt and for some reason the following code will create the desired text file, but never writes anything to it. Am I doing something wrong? I believe I've copied the example in the documentation pretty accurately.
qDebug() << output
works as expected, but even though the file is created, nothing is ever written to it.
#include <QCoreApplication>
#include <QtDebug>
#include <QString>
#include <QDateTime>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString filename = "";
filename.append(QString::number(QDateTime::currentMSecsSinceEpoch()));
filename.append(".txt");
QFile file(filename);
file.open(QIODevice::WriteOnly);
QTextStream out(&file);
QString output = "TEST";
qDebug() << output;
out << output;
return a.exec();
}
The data does not get written to disk immediately: It sits in a buffer until it's flushed.
Close the file after you've finished writing.
(In my experience, the file is closed anyway when you quit the program, but it's good practice to do this explicitly)

Resources