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. [Solved]Convert ASCII hex to int

[Solved]Convert ASCII hex to int

Scheduled Pinned Locked Moved General and Desktop
27 Posts 7 Posters 49.9k Views
  • 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.
  • V Offline
    V Offline
    VRonin
    wrote on 6 Mar 2018, 12:51 last edited by VRonin 3 Jun 2018, 13:14
    #16

    could you explain line by line

    Added comments, for more info just look here: http://doc.qt.io/qt-5/qregularexpression.html#global-matching

    On the other hand, when I put this code into mine I get a lot of errors

    what errors? if it's because you don't support C++11 for some reason I changed the code so it works on old compilers

    if( Eingang==("80:"))

    add a regular expression for that too:

    const QRegularExpression eingangExp(QStringLiteral("^\\s*([0-9a-fA-F]{2}):"));
    const QRegularExpression hexRegExp(QStringLiteral("[0-9a-fA-F]{2}(?!:)"));
    const QRegularExpressionMatch eingangMatch = eingangExp.match(line);
    if(eingangMatch.hasMatch()){
    if(eingangMatch.capturedRef(1).compare("80",Qt::CaseInsensitive)==0){ // Eingang==("80:")
    qDebug() << "Eingang is 80";
    for(QRegularExpressionMatchIterator i = hexRegExp.globalMatch(line);i.hasNext();){ // iterate over all the matches
    const QRegularExpressionMatch hexMatch = i.next();
    qDebug() << "Hex: " << hexMatch.capturedRef(0) << " Dec: " << hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16); //capturedRef contains the part matched by the regular expression
    }
    }
    else if(eingangMatch.capturedRef(1).compare("7D",Qt::CaseInsensitive)==0){ // Eingang==("7D:"){
    qDebug() << "Eingang is 7D";
    for(QRegularExpressionMatchIterator i = hexRegExp.globalMatch(line);i.hasNext();){ // iterate over all the matches
    const QRegularExpressionMatch hexMatch = i.next();
    qDebug() << "Hex: " << hexMatch.capturedRef(0) << " Dec: " << hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16); //capturedRef contains the part matched by the regular expression
    }
    }
    }
    

    QStringList fields = line.split("[\s]+");// not working

    You have to tell split you are using a regexp and escape the \: QStringList fields = line.split(QRegularExpression("\\s+"));

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mpergand
      wrote on 6 Mar 2018, 13:12 last edited by
      #17
      QString line="80: 1C 02 FC 85 FF 4F FF 1D 89 17 08 10 01 00 00 00 3B 87 69 00 82 00 48 09 88 10 00 00 ";
      QStringList fields=line.split(':');
      qDebug()<<"en-tĂȘte:"<<fields[0];
      QByteArray values=QByteArray::fromHex(fields[1].toLatin1());
      qDebug()<<"data:"<<values;
      

      en-tĂȘte: "80"
      data :"\x1C\x02\xFC\x85\xFFO\xFF\x1D\x89\x17\b\x10\x01\x00\x00\x00;\x87i\x00\x82\x00H\t\x88\x10\x00\x00"

      1 Reply Last reply
      3
      • L Offline
        L Offline
        Leopold
        wrote on 6 Mar 2018, 13:39 last edited by
        #18

        heureka
        VRonin's code works:
        QVector()
        "80: 1C 02 FC 85 FF 4F FF 1D 89 17 08 10 01 00 00 00 3B 87 69 00 82 00 48 09 88 10 00 00 "
        4
        QRegularExpressionMatch(Valid, has match: 0:(0, 3, "80:"), 1:(0, 2, "80"))
        "1C" 28
        "02" 2
        "FC" 252
        now I have to give eyh value to the different y
        but I think I can gigure that out.

        @mpergand
        this will divide first field from rest but how to divide the rest?
        y1= field1,y2=field2 and so on
        but I will test.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on 6 Mar 2018, 13:47 last edited by mpergand 3 Jun 2018, 13:47
          #19

          @Leopold said in [Solved]Convert ASCII hex to int:

          this will divide first field from rest but how to divide the rest?
          y1= field1,y2=field2 and so on
          but I will test.

          No need to split anything, you can access each byte in a QByteArray with at() or []
          See the doc ...

          1 Reply Last reply
          3
          • L Offline
            L Offline
            Leopold
            wrote on 6 Mar 2018, 21:18 last edited by
            #20

            @VRonin
            the example works great for transforming from hex to dec but i get the same row what i have in the txt now as a vektor. In my code you see that I have different y Vektors.
            I need value of field1 in y1, field2 in y2 and so on.I can not figure out how to take the i into a loop with y(i).

            V 1 Reply Last reply 7 Mar 2018, 08:28
            0
            • L Leopold
              6 Mar 2018, 21:18

              @VRonin
              the example works great for transforming from hex to dec but i get the same row what i have in the txt now as a vektor. In my code you see that I have different y Vektors.
              I need value of field1 in y1, field2 in y2 and so on.I can not figure out how to take the i into a loop with y(i).

              V Offline
              V Offline
              VRonin
              wrote on 7 Mar 2018, 08:28 last edited by
              #21

              @Leopold Every time you call i.next(); it moves to the next field in order so it's quite easy to manage

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Leopold
                wrote on 7 Mar 2018, 08:33 last edited by
                #22

                I tried this:
                { // iterate over all the matches

                										const QRegularExpressionMatch hexMatch = i.next();
                										qDebug() <<  hexMatch.capturedRef(0) <<  hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16); //capturedRef contains the part matched by the regular expression
                                                        y1.append(hexMatch.capturedRef(1).toUInt(Q_NULLPTR,16));
                                                         y2.append(hexMatch.capturedRef(2).toUInt(Q_NULLPTR,16));
                                                          y3.append(hexMatch.capturedRef(3).toUInt(Q_NULLPTR,16));
                                                           y4.append(hexMatch.capturedRef(4).toUInt(Q_NULLPTR,16));
                                                    }
                
                V 1 Reply Last reply 7 Mar 2018, 09:07
                0
                • L Leopold
                  7 Mar 2018, 08:33

                  I tried this:
                  { // iterate over all the matches

                  										const QRegularExpressionMatch hexMatch = i.next();
                  										qDebug() <<  hexMatch.capturedRef(0) <<  hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16); //capturedRef contains the part matched by the regular expression
                                                          y1.append(hexMatch.capturedRef(1).toUInt(Q_NULLPTR,16));
                                                           y2.append(hexMatch.capturedRef(2).toUInt(Q_NULLPTR,16));
                                                            y3.append(hexMatch.capturedRef(3).toUInt(Q_NULLPTR,16));
                                                             y4.append(hexMatch.capturedRef(4).toUInt(Q_NULLPTR,16));
                                                      }
                  
                  V Offline
                  V Offline
                  VRonin
                  wrote on 7 Mar 2018, 09:07 last edited by
                  #23

                  @Leopold said in [Solved]Convert ASCII hex to int:

                  { // iterate over all the matches

                  Look at that comment. Do you see what you are doing wrong?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Leopold
                    wrote on 7 Mar 2018, 10:01 last edited by VRonin 3 Jul 2018, 10:03
                    #24

                    yes I see that it is a loop but if i write :

                     if(eingangMatch.hasMatch())
                    								{
                    									if(eingangMatch.capturedRef(1).compare("80",Qt::CaseInsensitive)==0)
                    									{ // Eingang==("80:")
                    										qDebug() << eingangMatch;
                    										for(QRegularExpressionMatchIterator i = hexRegExp.globalMatch(line);
                    
                                                                i.hasNext();
                    
                    
                                                                )
                    
                    
                    											const QRegularExpressionMatch hexMatch = i.next();
                                                                qDebug() << hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16); //capturedRef contains the part matched by the regular expression
                                                                y1.append(hexMatch.capturedRef(1).toUInt(Q_NULLPTR,16));
                                                                 y2.append(hexMatch.capturedRef(2).toUInt(Q_NULLPTR,16));
                                                                  y3.append(hexMatch.capturedRef(3).toUInt(Q_NULLPTR,16));
                                                                   y4.append(hexMatch.capturedRef(4).toUInt(Q_NULLPTR,16));
                    
                    
                    
                                                    }
                    

                    the qDebug() << eingangMatch; and qDebug() << hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16);
                    don't work and all my y vectors get "0"
                    "80: 1C 05 3A 85 FF 4F FF 1D 89 1E 08 10 01 00 00 00 3B 87 51 01 AF 00 58 09 88 10 00 00 "
                    QVector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
                    QVector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

                    V 1 Reply Last reply 7 Mar 2018, 10:07
                    0
                    • L Leopold
                      7 Mar 2018, 10:01

                      yes I see that it is a loop but if i write :

                       if(eingangMatch.hasMatch())
                      								{
                      									if(eingangMatch.capturedRef(1).compare("80",Qt::CaseInsensitive)==0)
                      									{ // Eingang==("80:")
                      										qDebug() << eingangMatch;
                      										for(QRegularExpressionMatchIterator i = hexRegExp.globalMatch(line);
                      
                                                                  i.hasNext();
                      
                      
                                                                  )
                      
                      
                      											const QRegularExpressionMatch hexMatch = i.next();
                                                                  qDebug() << hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16); //capturedRef contains the part matched by the regular expression
                                                                  y1.append(hexMatch.capturedRef(1).toUInt(Q_NULLPTR,16));
                                                                   y2.append(hexMatch.capturedRef(2).toUInt(Q_NULLPTR,16));
                                                                    y3.append(hexMatch.capturedRef(3).toUInt(Q_NULLPTR,16));
                                                                     y4.append(hexMatch.capturedRef(4).toUInt(Q_NULLPTR,16));
                      
                      
                      
                                                      }
                      

                      the qDebug() << eingangMatch; and qDebug() << hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16);
                      don't work and all my y vectors get "0"
                      "80: 1C 05 3A 85 FF 4F FF 1D 89 1E 08 10 01 00 00 00 3B 87 51 01 AF 00 58 09 88 10 00 00 "
                      QVector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
                      QVector(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

                      V Offline
                      V Offline
                      VRonin
                      wrote on 7 Mar 2018, 10:07 last edited by VRonin 3 Jul 2018, 11:07
                      #25

                      @Leopold said in [Solved]Convert ASCII hex to int:

                      yes I see that it is a loop

                      I don't think you do...

                      int h=0;
                      for(QRegularExpressionMatchIterator i = hexRegExp.globalMatch(line);i.hasNext();++h){
                      const QRegularExpressionMatch hexMatch = i.next();
                      qDebug() << hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16); 
                      switch(h){
                      case 1 : y1.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;
                      case 2 : y2.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;
                      case 3 : y3.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;
                      case 4 : y4.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;
                      default: break;
                      }
                      }

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        Leopold
                        wrote on 7 Mar 2018, 10:44 last edited by
                        #26

                        i get error for the "i" no match for operator "++"
                        and the switch operator is not an integer
                        I copied the int i=0 as well
                        btw:my compiler is mingw9-32 from QT 5.6.2

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          Leopold
                          wrote on 7 Mar 2018, 10:53 last edited by
                          #27

                          the i was occupied by the "QRegularExpressionMatchIterator"
                          i changed int i to int h then ++h and switch h and it works.
                          Thank you great!

                          1 Reply Last reply
                          1

                          25/27

                          7 Mar 2018, 10:07

                          • Login

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