Qt lineEdit text conversion to hex issue - qt

I want to take the text from a lineEdit widget and convert it to hex.
I thought that the two following conversions have the same result but they don't.
1. data = QByteArray::fromHex(ui->lineEdit_send->text().toUtf8().toHex());
qDebug() << data;
2. data = QByteArray::fromHex(ui->lineEdit_send->text().toUtf8());
data = data.toHex();
qDebug() << data;
For example, if I enter the text "$54$65", the first implementation outputs $54$65 while the second implementation outputs "5465" and I cannot understand why the '$' is not displayed.

Related

Get the text data from QByteArray

I have a such problem:
I develop a simple database (PostgreSql).
One of the features that it keeps in column (column name: "ascii") a big text data (2000 rows) that reads from file (datatype in the table is BYTEA).
Here is the example of the code where I insert the data:
QFile *asciiFile = new QFile(ui->ascii_lineEdit->text());
asciiFile->open(QIODevice::ReadOnly);
QByteArray asciiArray = asciiFile->readAll();
qDebug() << asciiArray;
QSqlQuery *sourceQuery = new QSqlQuery();
sourceQuery->prepare("INSERT INTO source (image, image_material, archive, ascii, about) VALUES (:image, :image_material, :archive, :ascii, :about)");
sourceQuery->bindValue(":image", graphArray.toBase64());
sourceQuery->bindValue(":image_material", materialArray.toBase64());
sourceQuery->bindValue(":archive", archiveArray.toBase64());
sourceQuery->bindValue(":ascii", asciiArray.toBase64());
sourceQuery->bindValue(":about", ui->about_textEdit->toPlainText());
sourceQuery->exec();
Once more, the datatype of the column("ascii") that keeps is BYTEA.
Now I try to read this data (near 2000 lines) and they not displayed as well.
I try this method:
QByteArray asciiArray = QSqlQuery query = connector->getSourceAscii(item_id);
query.next();
QByteArray asciiArray = QByteArray::fromBase64(query.value("ascii").toByteArray());
QString *result = new QString(asciiArray);
qDebug() << *result;
It must be a lines of the digits but I have something like this:
�w߇�k��ۇ������z���������燸��wӇ�k�{�����w۝��};��{��x{��k�{k�{k�xk�x߾���{k�{�����������wㇸ��
The examples of the lines that I need:
2490 0,21979421377182 4,82690520584583E-02
2491 0,226718083024025 4,33071963489056E-02
Have you any suggestion about this? Thanks.

How to set character encoding for QTextBrowser in Qt?

I have a QTextBrowser in which I display the output contents of an external binary using QProcess in Linux. All is GOOD! But most of the contents are just boxes, so now it's the character encoding UTF-8 is missing and I need to tell this to the QTextBrowser. Is there any way for that?
The code:
....
processRAM = new QProcess();
processRAM->start("memtester", QStringList() << "1" << "1");
.....
connect(processRAM, SIGNAL(readyRead()),this,SLOT(displayRAMTestOutput()));
......
void MainWindow::displayRAMTestOutput()
{
textBrowserData->append(Qtring::fromUtf8(processRAM->readAllStandardOutput())));
}
I added the char encoding UTF-8 and still I see only boxes. What am I missing here?
You can set content of QTextBrowser in this way:
textBrowser->setText(QString::fromUtf8(processOutput)));
EDIT:
Your problem with "boxes" isn't connected with UTF8 encoding. Symbols which you see are control characters which are used by memtester when it displays text to console. If you don't want to display such characters in textBrowser, you can filter output:
while(!processRAM->atEnd())
{
QString out = QString::fromAscii(processRAM->readLine());
if(!out.contains("\b"))
textBrowser->append(out);}
}
\b means backspace which is displayed in you textBrowser as boxes.

Get a string representation of a single byte from QByteArray?

I have a QByteArray i create manually:
QByteArray hexArray(QByteArray::fromHex("495676"));
If this was encoded ASCII it would be "IVv".
If I want to get a single byte of data from that array.
I can do that like this:
qDebug() << messageToBeSent_raw[0];
However, that outputs I, which is correct but I would like to get 49. What I'm looking for is an equivalent of the QByteArray::toHex() just for a single byte. Is there a way to do it?
You can use QString::number.
qDebug() << QString::number(hexArray[0], 16);

qt creator mobile widget ui text edit new line?

i've started a mobile widget project
when a button is pushed the program does some astronomical calculations and then displays the results in a ui->text edit
so, the problem is that the calculations carry out two results
i want every result on a single how can it be done
p.s: i'm using
ui->textEdit->setPlainText(text)
there are no errors at all
i've tried
"/n"
std::endl
in the "/n" it write it as is
in std::endl it says:
error:
no match for 'operator+' in 'operator+(const char*, const QString&)(((const QString&)((const QString*)(& out1)))) + std::endl'
the code is:
QString out = "Alt : "+out1+std::endl+"Az : "+out2;
For the newline try this '\n'
Something like QString out = "Alt : "+out1+'\n'+"Az : "+out2; should work.

vector data check

hi a have a function that reads from a text file line by line each line I do some operations on it substitute a string..etc
then I push_back that line into a vector
this is my class in Parser.h
class Parser
{// start class
public:
vector<const char*> patterns;
Parser();
~Parser();
void RuleParser(const char *TextFileName); // this is the function that takes the file name
private:
};// end class
segment from function RuleParser
std::ifstream ifs(TextFileName);
while (!ifs.eof())
{
.
.modification code
.
patterns.push_back((buildString).c_str()); //buildString is the modified line
cout << buildString << endl;
}
but when I try to check out if the data in the vector is correct it output totally different data.
I even put a cout after the push_back to check it's integrty but I found buildString is correct... thats the data each time being pushed ... what I am doing wrong.
here is the loop I use to see if my data correct.
for (int i = 0;i < patterns.size() ;i++)
{
cout << patterns.at(i) << endl;
}
Well patterns is the collection of pointers so you end up push_back'ing a pointer to the same buildString in each iteration of the loop, instead of push_back'ing the string contents. Then when buildString changes in next iteration of the loop, the pointer becomes invalid but it still remains in patterns - not good
I suggest you declare patterns as:
vector<std::string> patterns;
This way when you do:
patterns.push_back(buildString.c_str())
the contents of the string will be copied instead of the pointer, and remain valid througout.

Resources