Conversion from a string to the ascii decimal value and get the total sum
-
Hi all, I'm starting to learn C ++ and Qt, I'm really a beginner.
I want to do a conversion from a string to the ascii decimal value and get the total sum.
Example:
String in: 12ab
Sum is: 294 (49 + 50 + 97 + 98)
I can't enter the ascii decimal value in the array.
Maybe it's not the right way, and I'm complicating everything, but it was for practice.I enclose the code
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_convert_clicked() { /* * conversion from "string" to "sum value ascii" */ QString StrIn = ui->lineEdit_string_in->text(); QStringList numberStrIn; for(const auto character: StrIn){ numberStrIn << QString::number(character.unicode(), 10); } QString StrOut = numberStrIn.join(" "); QString StrOut_01 = numberStrIn.join(", "); ui->lineEdit_test->setText(QString(StrOut)); /* * only for test */ qDebug() << "(StrIn) before: " << StrIn; qDebug() << "(numberStrIn) QStringList: " << numberStrIn; qDebug() << "(StrOut) : " << StrOut; qDebug() << "(StrOut_01) after for: " << StrOut_01; int length_1 = StrIn.length(); int length_2 = StrOut.length(); qDebug() << "length first string is: " << length_1; qDebug() << "length second string is: " << length_2; QString strOut_99(StrOut); QStringList list = strOut_99.split(" ",Qt::SkipEmptyParts); QString StrOut_numInt; foreach(QString StrOut_numInt, list) qDebug() << StrOut_numInt.toInt() << " .... StrOut_numInt.toInt()"; int arraylength = length_1; int testArray[arraylength] = { 0 }; // comment for test int i; int sumArray = 0; testArray[arraylength] = StrOut_numInt.toInt(); // comment for test // int testArray[arraylength] {49, 50, 97, 98}; // example.... (In string: 12ab) (Sum is: 294) for (int i=0; i<arraylength; i++) { sumArray += testArray[i]; } qDebug() << "sumArray is : " << sumArray; for (int i=0; i<arraylength; i++) { qDebug() << "test: " << i << " ... value: " << testArray[i]; } ui->lineEdit_int_out->setText(QString::number(sumArray)); }
-
QStringList list = strOut_99.split(" ",QString::SkipEmptyParts); int arraylength = list.size(); int testArray[arraylength]; for(int i=0; i<arraylength; i++) { testArray[i]=list[i].toInt(); } int sumArray = 0; for(int i=0; i<arraylength; i++) { sumArray+=testArray[i]; } qDebug()<<sumArray; // 294
-
You give me headache !
QString StrOut_numInt; foreach(QString StrOut_numInt, list) qDebug() << StrOut_numInt.toInt() << " .... StrOut_numInt.toInt()";
StrOut_numInt still empty !
testArray[arraylength] = StrOut_numInt.toInt();
You write 0 out of bound array !
-
sorry for the headache ...
but values come out of the qdebug outpud ...
qDebug () << StrOut_numInt.toInt () << ".... StrOut_numInt.toInt ()";
49 .... StrOut_numInt.toInt ()
50 .... StrOut_numInt.toInt ()testArray [arraylength] = StrOut_numInt.toInt ();
can you give me an example to use the string array to int correctly ... sorry I'm very confused
-
QStringList list = strOut_99.split(" ",QString::SkipEmptyParts); int arraylength = list.size(); int testArray[arraylength]; for(int i=0; i<arraylength; i++) { testArray[i]=list[i].toInt(); } int sumArray = 0; for(int i=0; i<arraylength; i++) { sumArray+=testArray[i]; } qDebug()<<sumArray; // 294