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.
  • L Offline
    L Offline
    leon.anavi
    wrote on 9 Sept 2013, 03:23 last edited by
    #2

    You can use "method toUInt of class QString to convert it to unsigned int":http://qt-project.org/doc/qt-5.0/qtcore/qstring.html#toUInt.

    @
    QString sValue = "FF";
    bool bStatus = false;
    uint nHex = sValue.toUInt(&bStatus,16);
    //Result: nHex == 255, bStatus == true
    @

    http://anavi.org/

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Ivan1120
      wrote on 10 Sept 2013, 13:25 last edited by
      #3

      Thanks : )

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on 10 Sept 2013, 13:26 last edited by
        #4

        [quote author="Ivan1120" date="1378819542"]Thanks : )[/quote]

        You are welcome :) Please edit the title of the thread and add [SOLVED] as a prefix.

        http://anavi.org/

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leopold
          wrote on 5 Mar 2018, 18:38 last edited by
          #5

          @leon.anavi said in [Solved]Convert ASCII hex to int:

          bool bStatus = false;

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Leopold
            wrote on 5 Mar 2018, 18:44 last edited by VRonin 3 Jun 2018, 09:01
            #6

            Hello,
            i have simular problem:
            i have a txt file with hex values in it, i want ro read them and store in a qvector.When I use your code I get error in qlist.h
            this is part of my code:

            				{
            								 bool bStatus = false;
                                             qDebug() << line;
            								 QStringList fields = line.split(' ');
            							     x.append(tick);
                                             tick++;
                                        
                                             y1.append(fields.at(1).toUInt(&bStatus,16));
                                             y2.append(fields.at(1).toUInt(&bStatus,16));
                                             y3.append(fields.at(1).toUInt(&bStatus,16));
            
            A 1 Reply Last reply 5 Mar 2018, 19:54
            0
            • L Leopold
              5 Mar 2018, 18:44

              Hello,
              i have simular problem:
              i have a txt file with hex values in it, i want ro read them and store in a qvector.When I use your code I get error in qlist.h
              this is part of my code:

              				{
              								 bool bStatus = false;
                                               qDebug() << line;
              								 QStringList fields = line.split(' ');
              							     x.append(tick);
                                               tick++;
                                          
                                               y1.append(fields.at(1).toUInt(&bStatus,16));
                                               y2.append(fields.at(1).toUInt(&bStatus,16));
                                               y3.append(fields.at(1).toUInt(&bStatus,16));
              
              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 5 Mar 2018, 19:54 last edited by
              #7

              Hi @Leopold,

              Unfortunately you didn't tell us which error you got.

              Nevertheless, there are several (potential) problems in your code.

              1. You split line in a QStringList fields, but don't check if the list contains more than one element before calling at(1). This can crash.
              2. You have a variable bStatus and give it to as Parameter to toUInt(), but don't check the value of bStatus
              3. You call the sequence fields.at(1).toUInt(&bStatus,16) three times. I'd store the result in a temporary value to avoid this duplication

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              2
              • L Offline
                L Offline
                Leopold
                wrote on 6 Mar 2018, 06:47 last edited by
                #8

                Hello,
                the error is a hint to line 531 in qlist.h
                "{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");"
                I know the fields are always the same so the number of y is the same too.Giving the same value to three different vectors in a while loop should not be the problem. But I will change that.

                A 1 Reply Last reply 6 Mar 2018, 07:22
                0
                • L Leopold
                  6 Mar 2018, 06:47

                  Hello,
                  the error is a hint to line 531 in qlist.h
                  "{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");"
                  I know the fields are always the same so the number of y is the same too.Giving the same value to three different vectors in a while loop should not be the problem. But I will change that.

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 6 Mar 2018, 07:22 last edited by
                  #9

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

                  Hello,
                  the error is a hint to line 531 in qlist.h
                  "{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");"

                  So take care to check fields.size() before calling fields.at(1). This should solve your problem.

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  2
                  • L Offline
                    L Offline
                    Leopold
                    wrote on 6 Mar 2018, 07:28 last edited by
                    #10

                    Hello,
                    my problem seems to be the line.split
                    QStringList fields = line.split(' ');
                    this is a file with ' ' separation
                    like "80: 1C 02 FC 85 FF 1D"

                    jsulmJ 1 Reply Last reply 6 Mar 2018, 07:30
                    0
                    • L Leopold
                      6 Mar 2018, 07:28

                      Hello,
                      my problem seems to be the line.split
                      QStringList fields = line.split(' ');
                      this is a file with ' ' separation
                      like "80: 1C 02 FC 85 FF 1D"

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on 6 Mar 2018, 07:30 last edited by
                      #11

                      @Leopold Can you do

                      qDebug() << line;
                      qDebug() << fields;
                      

                      and post the output here?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • L Offline
                        L Offline
                        Leopold
                        wrote on 6 Mar 2018, 08:57 last edited by
                        #12

                        In the meanwhile i have found how to split white space but it does not work.This is my split command:
                        QStringList fields = line.split("\s+");
                        this is qDebug()<<fields :("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 ")
                        what other split commands for white space are possible?

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on 6 Mar 2018, 09:08 last edited by VRonin 3 Jun 2018, 12:53
                          #13
                          const QRegularExpression hexRegExp(QStringLiteral("[0-9a-fA-F]{2}(?!:)"));  // regular expression that matches 2 hex digits NOT followed by :
                          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
                          }
                          

                          "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
                          3
                          • M Offline
                            M Offline
                            mpergand
                            wrote on 6 Mar 2018, 12:30 last edited by mpergand 3 Jun 2018, 12:31
                            #14

                            Using a QByteArray is the way to go !

                            QString hex="80 1c,02:fc 85, ff* 1000";  // the separator can be any non hex char
                            QByteArray values=QByteArray::fromHex(hex.toLatin1());
                            qDebug()<<values;
                            for(quint8 c : values)  // note the quint8
                              qDebug()<<c;
                            
                            1 Reply Last reply
                            1
                            • L Offline
                              L Offline
                              Leopold
                              wrote on 6 Mar 2018, 12:39 last edited by VRonin 3 Jun 2018, 12:51
                              #15

                              @ VRonin
                              sorry , could you explain line by line, i normaly can read c-code but this is too complex and I would like to understand.On the other hand, when I put this code into mine I get a lot of errors.Here is part of my code. I have to examine first field and according to its value read the lines into different vectors.
                              i need an expression that the line will be split,every ( to me) known didn't work up to know.
                              my code:

                              if (line_count>3) // read line from line 4
                              							{
                                                              QStringList fields = line.split("[\s]+");// not working
                                                                qDebug()<<fields;// shows complete line, should be first field
                                                               Eingang.append(fields.at(0));// its a QString declared earlier
                                                               qDebug()<<Eingang;             
                                                               if( Eingang==("80:"))// then read this line
                              								{
                              								 bool bStatus = false;
                                                               qDebug() << line;
                                                               x.append(tick); // this will be x-line in a plot
                                                               tick++;
                                                          
                                                               y1.append(fields.at(1).toUInt(&bStatus,16));// one graph inplot
                                                               y2.append(fields.at(2).toUInt(&bStatus,16));// second graph in plot
                                            
                              								}
                                                              else if ( Eingang== ("7D:"))
                              								{
                                                                  bool bStatus = false;
                                                                  qDebug() << line;
                                                                  QStringList fields = line.split("\\s+");// didn't work too
                                                                  x.append(tick);
                                                                  tick++;
                                                               y29.append(fields.at(1).toUInt(&bStatus,16));
                                                               y30.append(fields.at(2).toUInt(&bStatus,16));
                                                   
                              								}
                              							}
                              						}		 
                              

                              and here part of the file:

                              Running command: read-raw
                              ECU responded to D0 command with: 98 00 01 02
                              
                              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 
                              7D: 20 10 1C FF 92 00 A8 FF FF 01 01 7D 59 00 FF 51 FF FF 30 80 7F 7F FF 01 00 07 00 0D C0 1E 00 45 
                              80: 1C 03 0C 85 FF 4F FF 1E 89 17 08 10 01 00 00 00 3B 87 69 00 82 00 48 09 88 10 00 00 
                              7D: 20 10 1C FF 92 00 9D FF FF 01 01 7D 59 00 FF 51 FF FF 30 80 7F 6D FF 01 00 07 00 0D C0 1E 00 45 
                              80: 1C 02 F8 85 FF 4F FF 1E 89 17 08 10 01 00 00 00 3B 87 69 00 8C 00 48 09 88 10 00 00 
                              7D: 20 10 1C FF 92 00 43 FF FF 01 01 7D 58 00 FF 51 FF FF 30 80 7F 79 FF 01 00 07 00 0D C0
                              
                              1 Reply Last reply
                              0
                              • VRoninV Offline
                                VRoninV 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).

                                        VRoninV 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).

                                          VRoninV Offline
                                          VRoninV 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

                                          • Login

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