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. Tag - Length - Value parse in qt

Tag - Length - Value parse in qt

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 334 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by Damian7546
    #1

    Hi,

    I try understand below class:
    https://codereview.stackexchange.com/questions/115395/tlv-implementation-in-c
    to deserialize (from bytes array to TLV (tag - length - value) object).

    I run this class in my qt project in this way:

    QByteArray buff = "045002124354";
    std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size());
    
    TLVObject obj1;
    obj1.Deserialize(output); // Reconstruct TLV from byte array
    

    And doesn't work.... anyway.. How this class know which one is Tag and wchich one is value ?
    How can I access this data from the obj1 object?

    jsulmJ 1 Reply Last reply
    0
    • D Damian7546

      @jsulm You are right.
      But I do not still know why my app doesn't work.
      When I try run my app

      QByteArray buff = "045002124354";
          std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size());
      
          TLVObject obj1;
          obj1.Deserialize(output); // Reconstruct TLV from byte array
      

      I get error:
      terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 12) >= this->size() (which is 12)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #5

      @Damian7546
      How do you/we know that 045002124354 is a valid serialization for TLV to deserialize?? For example, are you sure these bytes are supposed to be decimal character digits, not actual binary values...?? Glancing --- and I do mean only glancing --- at that code I am unconvinced they are what they are supposed to be.

      1 Reply Last reply
      0
      • D Damian7546

        Hi,

        I try understand below class:
        https://codereview.stackexchange.com/questions/115395/tlv-implementation-in-c
        to deserialize (from bytes array to TLV (tag - length - value) object).

        I run this class in my qt project in this way:

        QByteArray buff = "045002124354";
        std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size());
        
        TLVObject obj1;
        obj1.Deserialize(output); // Reconstruct TLV from byte array
        

        And doesn't work.... anyway.. How this class know which one is Tag and wchich one is value ?
        How can I access this data from the obj1 object?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Damian7546 said in Tag - Length - Value parse in qt:

        How this class know which one is Tag and wchich one is value ?

        The code is there, why don't you read it?

        void  TLVObject::Deserialize(vector<uint8_t> value)
        {
            // Get type
            TLV_TYPE tlvType =  static_cast<TLV_TYPE>(value.at(0));
        
            // Get tag name size.
            vector<uint8_t> tmp;
            tmp.push_back(value.at(1));
            tmp.push_back(value.at(2));
            uint16_t tagLength = LEToUINT16(tmp);
        
            if(tagLength > MAXTAGLENGTH)
                throw exception("Tag name length exceeded");
        
            // Now get tag name
            std::string tagName;
            for(int i = 0; i<tagLength; i++)
                tagName += value.at(3 + i); // Use an offset.
        ...
        

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

        D 2 Replies Last reply
        0
        • jsulmJ jsulm

          @Damian7546 said in Tag - Length - Value parse in qt:

          How this class know which one is Tag and wchich one is value ?

          The code is there, why don't you read it?

          void  TLVObject::Deserialize(vector<uint8_t> value)
          {
              // Get type
              TLV_TYPE tlvType =  static_cast<TLV_TYPE>(value.at(0));
          
              // Get tag name size.
              vector<uint8_t> tmp;
              tmp.push_back(value.at(1));
              tmp.push_back(value.at(2));
              uint16_t tagLength = LEToUINT16(tmp);
          
              if(tagLength > MAXTAGLENGTH)
                  throw exception("Tag name length exceeded");
          
              // Now get tag name
              std::string tagName;
              for(int i = 0; i<tagLength; i++)
                  tagName += value.at(3 + i); // Use an offset.
          ...
          
          D Offline
          D Offline
          Damian7546
          wrote on last edited by Damian7546
          #3

          @jsulm You are right.
          But I do not still know why my app doesn't work.
          When I try run my app

          QByteArray buff = "045002124354";
              std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size());
          
              TLVObject obj1;
              obj1.Deserialize(output); // Reconstruct TLV from byte array
          

          I get error:
          terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 12) >= this->size() (which is 12)

          JonBJ 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Damian7546 said in Tag - Length - Value parse in qt:

            How this class know which one is Tag and wchich one is value ?

            The code is there, why don't you read it?

            void  TLVObject::Deserialize(vector<uint8_t> value)
            {
                // Get type
                TLV_TYPE tlvType =  static_cast<TLV_TYPE>(value.at(0));
            
                // Get tag name size.
                vector<uint8_t> tmp;
                tmp.push_back(value.at(1));
                tmp.push_back(value.at(2));
                uint16_t tagLength = LEToUINT16(tmp);
            
                if(tagLength > MAXTAGLENGTH)
                    throw exception("Tag name length exceeded");
            
                // Now get tag name
                std::string tagName;
                for(int i = 0; i<tagLength; i++)
                    tagName += value.at(3 + i); // Use an offset.
            ...
            
            D Offline
            D Offline
            Damian7546
            wrote on last edited by
            #4

            @jsulm for the help of this class is possible Parse data to object TLV form QbyteArray?
            Do you know any library to can do this ?

            1 Reply Last reply
            0
            • D Damian7546

              @jsulm You are right.
              But I do not still know why my app doesn't work.
              When I try run my app

              QByteArray buff = "045002124354";
                  std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size());
              
                  TLVObject obj1;
                  obj1.Deserialize(output); // Reconstruct TLV from byte array
              

              I get error:
              terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 12) >= this->size() (which is 12)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @Damian7546
              How do you/we know that 045002124354 is a valid serialization for TLV to deserialize?? For example, are you sure these bytes are supposed to be decimal character digits, not actual binary values...?? Glancing --- and I do mean only glancing --- at that code I am unconvinced they are what they are supposed to be.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                stryga42
                wrote on last edited by stryga42
                #6

                JonB is completely right. You initialize the QByteArray with "numerical garbage" and so the de-serialization fails. Details: If you initialize QByteArray with a const char * you will get the ASCII codes as values. A "0" will thus yield a value of 48, which is definitely not a valid enum TLV_TYPE. And even if it would be a numerical zero, it would decode to TLV_TYPE::UNDEFINED, which doesn't sound reasonable. Please use data which was generated from a serialization.

                1 Reply Last reply
                2
                • D Damian7546 has marked this topic as solved on

                • Login

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