Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. how to read hex value data from text file and store into array?
Forum Updated to NodeBB v4.3 + New Features

how to read hex value data from text file and store into array?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qt4
19 Posts 5 Posters 15.6k Views 3 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    So you basically want to just remove the first 15bytes of the line ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

      no i want to just copy this 498 bytes data into an array...

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #7

        So basically:

        QByteArray, fileContent = myFile.readAll();
        memcpy(&hex[4], fileContent.data(), fileContent.size());
        

        ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        JohanSoloJ 1 Reply Last reply
        0
        • SGaistS SGaist

          So basically:

          QByteArray, fileContent = myFile.readAll();
          memcpy(&hex[4], fileContent.data(), fileContent.size());
          

          ?

          JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by JohanSolo
          #8

          @SGaist said:

          So basically:

          QByteArray, fileContent = myFile.readAll();
          memcpy(&hex[4], fileContent.data(), fileContent.size());
          

          ?

          I don't think so, he apparently has the binary values he's interesting in ASCII format in the file.

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sachin786
            wrote on last edited by
            #9

            i have open and read this file properly in c but for storing this data in array not get it any idea...

            #include <stdio.h>
            #include <stdlib.h>
            int main()
            {
            FILE *f;
            char c;
            f=fopen("1.txt","r");
            while((c=fgetc(f))!=EOF)
            {
            printf("%c",c);
            }
            fclose(f);
            return 0;
            }

            1 Reply Last reply
            0
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #10

              Read the file content in a QByteArray using QFile::readAll().
              Then call split(',') on that byte array.
              You will get a list of QByteArray like:

              ["0x09","0xFF","0xD5","0x3C","0x06","0x7C","0x0A"...]
              

              Now you can loop over all the elements of the list and call QByteArray::fromHex(...).toInt() to convert from hex to integer.

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

              JohanSoloJ 1 Reply Last reply
              1
              • jsulmJ jsulm

                Read the file content in a QByteArray using QFile::readAll().
                Then call split(',') on that byte array.
                You will get a list of QByteArray like:

                ["0x09","0xFF","0xD5","0x3C","0x06","0x7C","0x0A"...]
                

                Now you can loop over all the elements of the list and call QByteArray::fromHex(...).toInt() to convert from hex to integer.

                JohanSoloJ Offline
                JohanSoloJ Offline
                JohanSolo
                wrote on last edited by
                #11

                @jsulm said:

                Read the file content in a QByteArray using QFile::readAll().
                Then call split(',') on that byte array.

                Nice idea, no need for regular expressions indeed!

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sachin786
                  wrote on last edited by sachin786
                  #12

                  ok for now i done till this.
                  my file is 1.txt, which contains simply this "1A,2C,0A"

                  unsigned char temp[3]={0}
                  QByteArray bb("0x");
                  QByteArray aa;

                  QString line;
                  QFile file1(filename);
                  if(file1.open(QIODevice::ReadOnly | QIODevice::Text)){
                  line=file1.readAll();
                  QStringList strings2 = line.split(QRegExp("\W+"), QString::SkipEmptyParts);
                  int n = strings2.size();

                      for(x=0;x<n;x++){
                             qDebug() << strings2.at(x) << "  ";
                             aa = strings2.at(x).toLocal8Bit();      // at this aa =1A first loop, and so on.
                             bb=bb.append(aa);    // bb= 0x1A for first loop , and so on
                             bb ="0x";                     // initialise 'bb'
                             /*
                              *
                              * in this portion is there any way  i can save value of 'bb' to my unsigned    char  temp[ ]?
                              *so that i can do something like temp[x]=bb ?? 
                              */
                  
                             if(x==n) break;
                            }               
                       }
                  
                  1 Reply Last reply
                  0
                  • jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #13

                    It is not really clear what you want: do you want to store a character represented by the hex value or the integer value?
                    So, how should temp look after you store bb there?
                    What is this supposed to do?

                    bb=bb.append(aa);    // bb= 0x1A for first loop , and so on
                    bb ="0x";
                    

                    you first assign a value and then overwrite it with "0x".
                    Why do you need 0x?

                    In my previous post I already suggested how to convert hex string to integer.

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

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      From your latest text sample, what about:

                      QByteArray dataFromFile = file.readAll();
                      QByteArray aa = "0x" +  dataFromFile.replace(",", "");
                      

                      But as @jsulm asked, what is your final goal with these data ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sachin786
                        wrote on last edited by sachin786
                        #15
                        1. I have a file with hex values saved as hex.txt which has
                          0x04,0x2C,0x64,0x00,0xB6,0x03,0xFA,0x84,0x87,0x6A,0x74,0x18,0x01,0x64,0x79,0x13,0x09,0x6A,0xCB,0xED,0x3D,0x9C,0x79,0x88,0x49,0x4A,0x2E,0xE8,0x89,0x24,0xF7,0xDD,0x04,0xA9,0x09,0xE7,0xE5,0x14,0xF7,0x5B,0xC5,0x49,0x38,0x10,0xFA,0x5C,0xFA,0x99,0xC4,0x0D,0x74,0xE0,0x3A,0x45,0xE8,0x64,0xCC,0xBB,0x72,0x57,0x31,0x63,0xF9,0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0xB5,0x50,0xF7,0x87,0x85,0x17,0x40,0xE8,0x01,0x98,0x4A,0x86,0xC7,0xF7,0xFF,0x0F,0x31,0x38,0x4A,0x0B,0x44,0x38,0x7E,0xF8,0x59,0xE8,0x49,0x8B,0xC3,0xD9,0xFF,0xEF,0x72,0xE0,0x4A,0x0C,0xC7,0x29,0x3E,0xF8,0x45,0x33,0x7C,0x1B,0x02,0xDA,0x3F,0xF8,0xB1,0xF8,0x45,0x80,0x44,0x19,0xBE,0xE0,0xD6,0xC8,0x45,0x04,0xC3,0xF9,0x05,0x1F,0xA1,0x6B,0x7C,0x94,0xC3,0xF7,0xFB,0x0F,0xA3,0x2F,0x3F,0x11,0x27,0x42,0xF7,0x44,0x1F,0x65,0x62,0x52,0x42,0x4A,0x34,0x22,0x13,0x21,0x54,0x18,0x22,0x85,0xFF,0x43,0x32,0xB8,0xA5,0xF7,0xFF,0x12,0xF2,0x1F,0xC1,0x68,0x48,0x86,0x2F,0x22,0xF8,0x26,0x23,0xFF,0x0F,0xF6,0xFF,0x3F,0x77,0x45,0x1F,0x1F,0x17,0xF4,0x74,0x34,0x36,0x73,0x3F,0x26,0xF2,0x1F,0x6F,0x92,0x41,0xF7,0xFF,0x77,0xA6,0xF4,0x8F,0x4F,0xF4,0xF9,0x33,0x75,0x32,0xF4,0xF1,0xF8,0xF1,0x1F,0x8F,0x46,0x36,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA2,0xE4
                          total is 498 bytes

                        2)Now I need to copy this 498 bytes values (same to same) to below array at a particular position starting from [8] to [498]

                        given the array in that array i want to store above text file data.....
                        unsigned char hex[508]={
                        0x5A,0xA5,0x0B,0x01,0xF4,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

                        kshegunovK 1 Reply Last reply
                        0
                        • jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16
                          temp[x]=static_cast<unsigned char>(QByteArray::fromHex(bb).toInt());
                          

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

                          1 Reply Last reply
                          0
                          • S sachin786
                            1. I have a file with hex values saved as hex.txt which has
                              0x04,0x2C,0x64,0x00,0xB6,0x03,0xFA,0x84,0x87,0x6A,0x74,0x18,0x01,0x64,0x79,0x13,0x09,0x6A,0xCB,0xED,0x3D,0x9C,0x79,0x88,0x49,0x4A,0x2E,0xE8,0x89,0x24,0xF7,0xDD,0x04,0xA9,0x09,0xE7,0xE5,0x14,0xF7,0x5B,0xC5,0x49,0x38,0x10,0xFA,0x5C,0xFA,0x99,0xC4,0x0D,0x74,0xE0,0x3A,0x45,0xE8,0x64,0xCC,0xBB,0x72,0x57,0x31,0x63,0xF9,0x7F,0x46,0x48,0xF4,0xE8,0x91,0x5B,0xF8,0xED,0x07,0x9B,0x09,0x07,0x0D,0x64,0xF7,0x64,0xC7,0x5A,0xB8,0x17,0xCD,0xBC,0x6A,0xA9,0xC3,0x9D,0xEF,0x96,0x9D,0x62,0xF5,0xDD,0x83,0xD9,0x45,0x0F,0x99,0x4B,0xF6,0x63,0x41,0x29,0x38,0xE0,0x4A,0xCD,0xF9,0x94,0xC3,0x1D,0xAE,0xB8,0x96,0x3D,0x69,0x82,0x8C,0xFF,0x29,0xF9,0x2A,0xF6,0x85,0x50,0x45,0x1A,0x7E,0xF8,0x26,0x66,0x86,0x43,0x48,0x4B,0x3E,0xF9,0x96,0xD4,0x6A,0x33,0xC3,0x6B,0x75,0xFD,0x29,0x95,0xAA,0x27,0xC4,0xBD,0xF7,0x0F,0x99,0x1D,0xEB,0x9C,0x84,0xFB,0x7B,0xF8,0x4D,0x3A,0xFA,0x12,0x43,0xC9,0x43,0x0F,0x66,0x52,0xF7,0x6F,0x87,0x4A,0x36,0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0x3F,0x77,0x45,0x1F,0x1F,0x17,0xF4,0x74,0x34,0x36,0x73,0x3F,0x26,0xF2,0x1F,0x6F,0x92,0x41,0xF7,0xFF,0x77,0xA6,0xF4,0x8F,0x4F,0xF4,0xF9,0x33,0x75,0x32,0xF4,0xF1,0xF8,0xF1,0x1F,0x8F,0x46,0x36,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA2,0xE4
                              total is 498 bytes

                            2)Now I need to copy this 498 bytes values (same to same) to below array at a particular position starting from [8] to [498]

                            given the array in that array i want to store above text file data.....
                            unsigned char hex[508]={
                            0x5A,0xA5,0x0B,0x01,0xF4,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by kshegunov
                            #17

                            @sachin786
                            Judging from your C code you don't have a text file at all but rather a binary file where the characters are saved. Assuming this is the case you read the data into a byte array (as already suggested) and just copy the relevant parts into a new byte array.

                            QFile inputFile("myFileName.txt", QFile::ReadOnly);
                            if (!inputFile.open())
                                ; //< Handle open errors here
                            
                            QByteArray binary = inputFile.readAll();   //< binary contains the file now
                            
                            unsigned char hex[508]; //< This is your array
                            
                            qint32 start = 4, size = 498;
                            ::memcpy(hex + start, binary.constData(), size); //< Copy the binary data
                            

                            If, on the other hand, you indeed have a text file as shown in the above post, then you read the text into a string (as already suggested yet again) and split by the comma separator. You then convert the resulting strings into one byte integers and copy them over to the new byte array.

                            QFile inputFile("myFileName.txt", QFile::ReadOnly | QFile::Text);
                            if (!inputFile.open())
                                ; //< Handle open errors here
                            
                            QTextStream in(&inputFile);
                            QString text = in.readAll();   //< text contains the file now
                            
                            QStringList bytes = text.split(',');  //< Split the text into bytes
                            
                            QByteArray binary; //< Prepare the byte array
                            binary.reserve(bytes.size());
                            
                            bool ok = true;
                            foreach (QString byte, bytes)  {
                                unsigned char hexChar = byte.toInt(&ok, 0); //< Convert string to integer according to C-style rules
                                if (!ok)
                                    ; //< Error while converting, handle accordingly
                            
                                binary.append(hexChar); //< Append the char to the byte array
                            }
                            
                            // And finally, copy the data:
                            
                            unsigned char hex[508]; //< This is your array
                            
                            qint32 start = 4, size = 498;
                            ::memcpy(hex + start, binary.constData(), size); //< Copy the binary data
                            

                            EDIT: Alternatively the last snippet could be modified so you can save the data directly while converting:

                            QFile inputFile("myFileName.txt", QFile::ReadOnly | QFile::Text);
                            if (!inputFile.open())
                                ; //< Handle open errors here
                            
                            QTextStream in(&inputFile);
                            QString text = in.readAll();   //< text contains the file now
                            
                            QStringList bytes = text.split(',');  //< Split the text into bytes
                            
                            bool ok = true;
                            
                            qint32 start = 4, size = 498;
                            for (qint32 i = 0, bytesNumber = bytes.size(), offset = start; i < bytesNumber && i < size; i++, offset++)  {
                                unsigned char hexChar = byte.toInt(&ok, 0); //< Convert string to integer according to C-style rules
                                if (!ok)
                                    ; //< Error while converting, handle accordingly
                            
                                hex[offset] = hexChar;
                            }
                            

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            1
                            • S Offline
                              S Offline
                              sachin786
                              wrote on last edited by
                              #18

                              @kshegunov thanks for reply...
                              #include "qtquick1applicationviewer.h"
                              #include <QApplication>
                              #include <QtCore>
                              #include <QDebug>
                              int main(int argc, char *argv[])
                              {
                              QApplication app(argc, argv);

                              QtQuick1ApplicationViewer viewer;
                              viewer.addImportPath(QLatin1String("modules"));
                              viewer.setOrientation(QtQuick1ApplicationViewer::ScreenOrientationAuto);
                              viewer.setMainQmlFile(QLatin1String("qrc:/main.qml"));
                              viewer.showExpanded();
                              
                              
                              bool ok = true;
                              QString filename="/home/anas/myQtApp/sachin2hex/hex.txt";
                              QFile inputFile(filename);
                              QByteArray binary; //< Prepare the byte array
                              QStringList bytes;
                              int i=0;
                              
                              unsigned char hex[508]={0x5A,0xA5,0x0B,0x01,0xF4,0x01,0x00,0x00,....position from[4] to [498] bytes data ///  << same as mention above
                              
                              if(inputFile.open(QIODevice::ReadOnly | QIODevice::Text)){
                                  QTextStream in(&inputFile);
                                  QString text = in.readAll();   //< text contains the file now
                              
                                  bytes = text.split(',');
                                  qDebug() << "bytes :: " << bytes;
                              
                                  binary.reserve(bytes.size());
                              
                                  foreach (QString byte, bytes)  {
                                      unsigned char hexChar = byte.toInt(&ok, 0); //< Convert string to integer according to C-style rules
                                      if (!ok)
                                          ; //< Error while converting, handle accordingly
                              
                                      binary.append(hexChar); //< Append the char to the byte array
                                  }
                              
                                  qint32 start = 4, size = 498;
                                  ::memcpy(hex + start, binary.constData(), size); //< Copy the binary data
                              
                              
                              }
                              
                              for(i=0;i<508;i++) printf("%c", hex[i]);
                              
                              return app.exec();
                              

                              }

                              it print garbage for hex[ ]......

                              kshegunovK 1 Reply Last reply
                              0
                              • S sachin786

                                @kshegunov thanks for reply...
                                #include "qtquick1applicationviewer.h"
                                #include <QApplication>
                                #include <QtCore>
                                #include <QDebug>
                                int main(int argc, char *argv[])
                                {
                                QApplication app(argc, argv);

                                QtQuick1ApplicationViewer viewer;
                                viewer.addImportPath(QLatin1String("modules"));
                                viewer.setOrientation(QtQuick1ApplicationViewer::ScreenOrientationAuto);
                                viewer.setMainQmlFile(QLatin1String("qrc:/main.qml"));
                                viewer.showExpanded();
                                
                                
                                bool ok = true;
                                QString filename="/home/anas/myQtApp/sachin2hex/hex.txt";
                                QFile inputFile(filename);
                                QByteArray binary; //< Prepare the byte array
                                QStringList bytes;
                                int i=0;
                                
                                unsigned char hex[508]={0x5A,0xA5,0x0B,0x01,0xF4,0x01,0x00,0x00,....position from[4] to [498] bytes data ///  << same as mention above
                                
                                if(inputFile.open(QIODevice::ReadOnly | QIODevice::Text)){
                                    QTextStream in(&inputFile);
                                    QString text = in.readAll();   //< text contains the file now
                                
                                    bytes = text.split(',');
                                    qDebug() << "bytes :: " << bytes;
                                
                                    binary.reserve(bytes.size());
                                
                                    foreach (QString byte, bytes)  {
                                        unsigned char hexChar = byte.toInt(&ok, 0); //< Convert string to integer according to C-style rules
                                        if (!ok)
                                            ; //< Error while converting, handle accordingly
                                
                                        binary.append(hexChar); //< Append the char to the byte array
                                    }
                                
                                    qint32 start = 4, size = 498;
                                    ::memcpy(hex + start, binary.constData(), size); //< Copy the binary data
                                
                                
                                }
                                
                                for(i=0;i<508;i++) printf("%c", hex[i]);
                                
                                return app.exec();
                                

                                }

                                it print garbage for hex[ ]......

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by
                                #19

                                @sachin786

                                it print garbage for hex[ ]......

                                What do you expect to be printed exactly? The data is binary so any of the elements that can't be displayed will be printed as gibberish ... 0x01, 0x00 are non-printable characters, so you'd see (depending on the platform) squares or other strange symbols, this doesn't necessarily mean the data is corrupted, only that it's interpreted in a erroneous way.

                                Read and abide by the Qt Code of Conduct

                                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