Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Conversion from a string to the ascii decimal value and get the total sum
Forum Updated to NodeBB v4.3 + New Features

Conversion from a string to the ascii decimal value and get the total sum

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 615 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gede
    wrote on 20 Aug 2022, 13:04 last edited by
    #1

    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));
    }
    
    

    AddArray.JPG

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 20 Aug 2022, 16:09 last edited by mpergand
      #4
          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
      
      1 Reply Last reply
      1
      • M Offline
        M Offline
        mpergand
        wrote on 20 Aug 2022, 13:54 last edited by mpergand
        #2

        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 !

        1 Reply Last reply
        1
        • G Offline
          G Offline
          gede
          wrote on 20 Aug 2022, 15:49 last edited by
          #3

          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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on 20 Aug 2022, 16:09 last edited by mpergand
            #4
                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
            
            1 Reply Last reply
            1
            • G Offline
              G Offline
              gede
              wrote on 20 Aug 2022, 16:59 last edited by
              #5

              I never thought I'd use:

                       int arraylength = list.size ();
              
                       for (int i = 0; i <arraylength; i ++)
                           {
                           testArray [i] = list [i] .toInt ();
                           }
              

              I'm still too new, but I'm trying.

              It works. Thank you very much

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on 20 Aug 2022, 17:25 last edited by mpergand
                #6

                A version that give me less headache ;)

                QString str="12ab";
                int sum=0;
                QString ascii;
                
                for(QChar c : str)
                   {
                   ushort u=c.unicode();
                   sum+=u;
                   ascii.append(QString("%1 ").arg(u));
                   }
                qDebug()<<"sum ="<<sum;
                qDebug()<<"ascii values ="<<ascii;
                
                1 Reply Last reply
                0

                1/6

                20 Aug 2022, 13:04

                • Login

                • Login or register to search.
                1 out of 6
                • First post
                  1/6
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved