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. how to read and parse hex value data from text file
Forum Updated to NodeBB v4.3 + New Features

how to read and parse hex value data from text file

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.9k 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.
  • K KARMA

    I have a text file where i store my hex data.
    I store every package I receive from the serial, line by line.

    The records in the text file are as follows;
    ffc48e3a0105038484848484848484
    ffb88f3a0105038403000000000000

    my code is as follows;

       QString filename= QFileDialog::getOpenFileName(this, "Choose File");
       QFile file(filename);
    
        if(filename.isEmpty())
        {
            return;
        }
        else if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
        {
           return;
        }
        else
        {
            QByteArray ba = file.readAll();
        }
    

    When I read the file, it saves it in qbytearray like this;
    Screenshot from 2022-04-14 15-18-16.png
    but I want it to save 2 letters instead of one letter in each index.

    for example;
    ba[0] = ff;
    ba[1] = 07;

    can you help me how to do it?

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #2

    @KARMA change your debug display

    8f1904a5-83c9-4979-b90a-b83ebd37eec3-image.png


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    K 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      @KARMA change your debug display

      8f1904a5-83c9-4979-b90a-b83ebd37eec3-image.png

      K Offline
      K Offline
      KARMA
      wrote on last edited by
      #3

      @J-Hilk I did, but nothing changed in the view.
      QByteArray puts each letter in a different index. I don't want this to happen because every 2 letters is a hex data.

      J.HilkJ 1 Reply Last reply
      0
      • K KARMA

        @J-Hilk I did, but nothing changed in the view.
        QByteArray puts each letter in a different index. I don't want this to happen because every 2 letters is a hex data.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #4

        @KARMA than the normal debug plane is not the right place for you, you'll need to write something your self and output it to the console or something like that


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KARMA
          wrote on last edited by KARMA
          #5

          @J-Hilk this is not the problem.
          "ff" is my packet head.
          Since "ff" is in separate indexes as 0 and 1, I cannot parse it.
          if i do pckt=ba[0] i just get the letter f.
          If I do memcpy(&pckt,&ba.data[0],2) I get a different value than 255. because ff isn't actually 1 byte not 2 byte.

          J.HilkJ 1 Reply Last reply
          0
          • K KARMA

            @J-Hilk this is not the problem.
            "ff" is my packet head.
            Since "ff" is in separate indexes as 0 and 1, I cannot parse it.
            if i do pckt=ba[0] i just get the letter f.
            If I do memcpy(&pckt,&ba.data[0],2) I get a different value than 255. because ff isn't actually 1 byte not 2 byte.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @KARMA said in how to read and parse hex value data from text file:

            @J-Hilk this is not the problem.
            "ff" is my packet head.
            Since "ff" is in separate indexes as 0 and 1, I cannot parse it.
            if i do pckt=ba[0] i just get the letter f.
            If I do memcpy(&pckt,&ba.data[0],2) I get a different value than 255. because ff isn't actually 1 byte not 2 byte.

            no,

            F is a nibble, aka 4 bit
            FF is a byte aka 8 bit
            QByteArray stores internally a char array. char = 8 bit = 1 byte = int8_t


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            K 1 Reply Last reply
            1
            • J.HilkJ J.Hilk

              @KARMA said in how to read and parse hex value data from text file:

              @J-Hilk this is not the problem.
              "ff" is my packet head.
              Since "ff" is in separate indexes as 0 and 1, I cannot parse it.
              if i do pckt=ba[0] i just get the letter f.
              If I do memcpy(&pckt,&ba.data[0],2) I get a different value than 255. because ff isn't actually 1 byte not 2 byte.

              no,

              F is a nibble, aka 4 bit
              FF is a byte aka 8 bit
              QByteArray stores internally a char array. char = 8 bit = 1 byte = int8_t

              K Offline
              K Offline
              KARMA
              wrote on last edited by
              #7

              @J-Hilk so how do i do that? for example I need to copy the first 4 bytes after ff to uint32, but when stored this way I can't. how should i go about it?

              1 Reply Last reply
              0
              • K KARMA

                I have a text file where i store my hex data.
                I store every package I receive from the serial, line by line.

                The records in the text file are as follows;
                ffc48e3a0105038484848484848484
                ffb88f3a0105038403000000000000

                my code is as follows;

                   QString filename= QFileDialog::getOpenFileName(this, "Choose File");
                   QFile file(filename);
                
                    if(filename.isEmpty())
                    {
                        return;
                    }
                    else if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
                    {
                       return;
                    }
                    else
                    {
                        QByteArray ba = file.readAll();
                    }
                

                When I read the file, it saves it in qbytearray like this;
                Screenshot from 2022-04-14 15-18-16.png
                but I want it to save 2 letters instead of one letter in each index.

                for example;
                ba[0] = ff;
                ba[1] = 07;

                can you help me how to do it?

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

                @KARMA said in how to read and parse hex value data from text file:

                I store every package I receive from the serial, line by line.

                Since you say, and show, "line by line", you will have to deal with (skip) the newlines/line breaks anyway. With readAll() you will have to do that, apart from having to go through a pair of bytes at a a time.

                You might prefer to wrap the QFile into a QTextStream so that you can use QString QTextStream::readLine() repeatedly to get each line with the newline removed. That's a QString, so you can go through it 2 characters at a time for your pairs of hex characters.

                Given a pair of characters, e.g. "ff" or "07", you can then use int QString::toInt(nullptr, 16) const to get the numeric value 0--255/0x00--0xFF of the character pair, if you need that, which it sounds like you do.

                Of course, if you don't want to do it via all these Qt methods you can always convert a pair of hexadecimal digit characters/bytes to the hex number yourself, with a single line of subtraction, multiplication and addition....

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #9
                      QByteArray data("ffc48e3a0105038484848484848484");  //  the data are in hexa string representation
                      QByteArray bin=QByteArray::fromHex(data);   // convert to binary
                  
                      QBuffer buffer(&bin);
                      buffer.open(QBuffer::ReadWrite);
                      QDataStream stream(&buffer);
                  
                      stream.skipRawData(1);      // skip first byte ff
                      uint32_t v1;
                      stream>>v1;      // read uint32
                      // assuming the next data is an int16 as an example
                      int16_t v2;
                      stream>>v2;      // read int16
                      etc ...
                  
                      qDebug()<<v1<<v2;   // 3297655297 1283
                  

                  You may have to consider endianess, here big endian (default)
                  see setByteOrder(QDataStream::ByteOrder bo)

                  JonBJ K 2 Replies Last reply
                  3
                  • M mpergand
                        QByteArray data("ffc48e3a0105038484848484848484");  //  the data are in hexa string representation
                        QByteArray bin=QByteArray::fromHex(data);   // convert to binary
                    
                        QBuffer buffer(&bin);
                        buffer.open(QBuffer::ReadWrite);
                        QDataStream stream(&buffer);
                    
                        stream.skipRawData(1);      // skip first byte ff
                        uint32_t v1;
                        stream>>v1;      // read uint32
                        // assuming the next data is an int16 as an example
                        int16_t v2;
                        stream>>v2;      // read int16
                        etc ...
                    
                        qDebug()<<v1<<v2;   // 3297655297 1283
                    

                    You may have to consider endianess, here big endian (default)
                    see setByteOrder(QDataStream::ByteOrder bo)

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

                    @mpergand said in how to read and parse hex value data from text file:

                    QByteArray bin=QByteArray::fromHex(data); // convert to binary

                    Sigh, forgot there is a QByteArray::fromHex(). So many little functions in Qt to keep in mind... :)

                    1 Reply Last reply
                    0
                    • M mpergand
                          QByteArray data("ffc48e3a0105038484848484848484");  //  the data are in hexa string representation
                          QByteArray bin=QByteArray::fromHex(data);   // convert to binary
                      
                          QBuffer buffer(&bin);
                          buffer.open(QBuffer::ReadWrite);
                          QDataStream stream(&buffer);
                      
                          stream.skipRawData(1);      // skip first byte ff
                          uint32_t v1;
                          stream>>v1;      // read uint32
                          // assuming the next data is an int16 as an example
                          int16_t v2;
                          stream>>v2;      // read int16
                          etc ...
                      
                          qDebug()<<v1<<v2;   // 3297655297 1283
                      

                      You may have to consider endianess, here big endian (default)
                      see setByteOrder(QDataStream::ByteOrder bo)

                      K Offline
                      K Offline
                      KARMA
                      wrote on last edited by
                      #11

                      @mpergand thank you so much. this worked great for me.
                      @JonB @J-Hilk thank you too

                      1 Reply Last reply
                      1

                      • Login

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