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] Dumping values in Vector Buffer
Forum Updated to NodeBB v4.3 + New Features

[solved] Dumping values in Vector Buffer

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.5k 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.
  • A Offline
    A Offline
    akshay123
    wrote on 3 Feb 2017, 10:08 last edited by akshay123 2 Aug 2017, 04:46
    #1

    I am new to Qt , currently developing an application which will parse a XML file and dump the values in the vector Buffer .

    my XML file look like this

    <Root>
    <class name="ADC"      type="DWORD"  value="0xDEEDBEAF" />
    <class name="BDC"      type="WORD"   value="0xEFCDEG" />
    <class name="MDC"      type="BYTE"   value="0xEFCDEG" />
    </Root>
    

    we have one (uchar) vector (buffer) . where the values of the XML attributes needs to be dumped.

    vector<uchar> buf;
    The problem is that when we will get the value(0xDEEDBEAF,0xEFCDEG ...) of each attribute tag , QString is returned

    QString value = classElement.attribute("value"); //Value is of type QString.
    But i need to push the data in to (uchar) vector according to BYTE , WORD, DWORD .

    i researched in the internet , but i am not able to get any good source of converting QString to BYTE , QString to WORD , QString to DWORD.

    Any help to convert Qstring to BYTE,WORD,DWORD will be great help full. So that data can be pushed into uchar vector.

    Thanks

    Edit: Added code tags -- @Wieland

    J 1 Reply Last reply 3 Feb 2017, 12:19
    0
    • A akshay123
      3 Feb 2017, 10:08

      I am new to Qt , currently developing an application which will parse a XML file and dump the values in the vector Buffer .

      my XML file look like this

      <Root>
      <class name="ADC"      type="DWORD"  value="0xDEEDBEAF" />
      <class name="BDC"      type="WORD"   value="0xEFCDEG" />
      <class name="MDC"      type="BYTE"   value="0xEFCDEG" />
      </Root>
      

      we have one (uchar) vector (buffer) . where the values of the XML attributes needs to be dumped.

      vector<uchar> buf;
      The problem is that when we will get the value(0xDEEDBEAF,0xEFCDEG ...) of each attribute tag , QString is returned

      QString value = classElement.attribute("value"); //Value is of type QString.
      But i need to push the data in to (uchar) vector according to BYTE , WORD, DWORD .

      i researched in the internet , but i am not able to get any good source of converting QString to BYTE , QString to WORD , QString to DWORD.

      Any help to convert Qstring to BYTE,WORD,DWORD will be great help full. So that data can be pushed into uchar vector.

      Thanks

      Edit: Added code tags -- @Wieland

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Feb 2017, 12:19 last edited by
      #2

      @akshay123 After writing everything into this buffer, how do you know how to read it? I mean, how do you know whether you need to read DWORD, WORD or BYTE?
      Your buffer will look like: [DE, ED, BE, AF, EF, CD, EG, EF, CD, EG]
      Note: G is not valid hex number!
      Why not use QByteArray?
      To convert hex string to number use http://doc.qt.io/qt-5/qstring.html#toInt

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

      1 Reply Last reply
      2
      • A Offline
        A Offline
        akshay123
        wrote on 6 Feb 2017, 03:30 last edited by
        #3

        After writing on to buffer, We have another xml file which keeps tracks of indices , With the help of those indices we are going to read which is next element (WORD,DWORD or BYTE ) to be read . Yes G is not a valid Hex number , While posting there was a typo mistake .

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 6 Feb 2017, 04:30 last edited by dheerendra 2 Jun 2017, 04:31
          #4

          In addition to what previous ans look at the documentation of qstring.There are so many functions like toascii() tolatin() tolocal8bit() etc. They should help you.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          5
          • A Offline
            A Offline
            akshay123
            wrote on 6 Feb 2017, 10:26 last edited by
            #5

            @jsulm Thanks for your sugestion ,

            Currently i have values like this

            value="10"
            value="11"
            value="12"
            value="13"
            value="14"
            value="15"
            value="16"
            value="07"

            Those are in decimal . But when i tried to convert these to hex using Qstring.setnum( ). I am not able to get the hex values .

            Thye code sniped it like this

            QString value_SB = tempElement.attribute("value");
            bool ok1;
            int value_B1= value_SB.toInt(&ok1, 16);
            QString hex_value;
            hex_value.setNum(value_B1,16);
            int result1 = hex_value.toInt(&ok1, 16);
            buff.push_back((uchar)result1);

            But the output is same 10 11 12 13

            J 2 Replies Last reply 6 Feb 2017, 10:33
            0
            • A akshay123
              6 Feb 2017, 10:26

              @jsulm Thanks for your sugestion ,

              Currently i have values like this

              value="10"
              value="11"
              value="12"
              value="13"
              value="14"
              value="15"
              value="16"
              value="07"

              Those are in decimal . But when i tried to convert these to hex using Qstring.setnum( ). I am not able to get the hex values .

              Thye code sniped it like this

              QString value_SB = tempElement.attribute("value");
              bool ok1;
              int value_B1= value_SB.toInt(&ok1, 16);
              QString hex_value;
              hex_value.setNum(value_B1,16);
              int result1 = hex_value.toInt(&ok1, 16);
              buff.push_back((uchar)result1);

              But the output is same 10 11 12 13

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 6 Feb 2017, 10:33 last edited by
              #6

              @akshay123 Why do you want to convert to hex? There is no need for that.

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

              1 Reply Last reply
              0
              • A akshay123
                6 Feb 2017, 10:26

                @jsulm Thanks for your sugestion ,

                Currently i have values like this

                value="10"
                value="11"
                value="12"
                value="13"
                value="14"
                value="15"
                value="16"
                value="07"

                Those are in decimal . But when i tried to convert these to hex using Qstring.setnum( ). I am not able to get the hex values .

                Thye code sniped it like this

                QString value_SB = tempElement.attribute("value");
                bool ok1;
                int value_B1= value_SB.toInt(&ok1, 16);
                QString hex_value;
                hex_value.setNum(value_B1,16);
                int result1 = hex_value.toInt(&ok1, 16);
                buff.push_back((uchar)result1);

                But the output is same 10 11 12 13

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 6 Feb 2017, 10:34 last edited by
                #7

                @akshay123 Just do toInt() as it is decimal

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  akshay123
                  wrote on 6 Feb 2017, 10:37 last edited by
                  #8

                  @jsulm some numbers are in decimal format and some are hexadecimal .

                  I want all the numbers whould be in hexadecimal format

                  J 1 Reply Last reply 6 Feb 2017, 11:48
                  0
                  • A akshay123
                    6 Feb 2017, 10:37

                    @jsulm some numbers are in decimal format and some are hexadecimal .

                    I want all the numbers whould be in hexadecimal format

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 6 Feb 2017, 11:48 last edited by
                    #9

                    @akshay123 Then you need to know which number is in which format: decimal 10 is not the same as 10 in hex.

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

                    1 Reply Last reply
                    2
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on 6 Feb 2017, 12:15 last edited by VRonin 2 Jun 2017, 12:57
                      #10
                      bool valueInVector(QString val,vector<uchar>& buf){
                      int base=10;
                      if(val.leftRef(2).compare("0x",QString::CaseInsensitive)==0){
                      base=16;
                      val.remove(0,2);
                      }
                      bool check=true;
                      for(int i=0;i<val.size() && check;i+=2){
                      const uchar tempVal=val.midRef(i,2).toUInt(&check,base);
                      if(check)
                      buf.push_back(tempVal);
                      }
                      return check;
                      }
                      

                      "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

                      1/10

                      3 Feb 2017, 10:08

                      • Login

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