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. Displaying data in hex format
QtWS25 Last Chance

Displaying data in hex format

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 5.3k 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
    admkrk
    wrote on 15 Sept 2019, 00:58 last edited by
    #1

    I have seen similar questions asked many times, but they usually referred to reading or writing files. My ultimate goal is to basically create an assembler IDE for programming EEPROMs. I have done something similar to all the parts except creating a hex viewer/editor. In general, I have difficulty dealing text, or I should say picking formats to use.

    For my tests I am using a .hex file created in Atmel Studio, but I assume a .ith file from NASM will be the same. No matter what I try, it seems I cam only get the ASCII value to print.

    My latest attempt:

    void MainWindow::openHexFile(QString file)
    {
        QFile f(file);
        char fileData;
        QByteArray data;
        if(!f.open(QIODevice::ReadOnly))
        {
            return;
        }
    
        while(!f.atEnd())
        {
            f.read(&fileData, sizeof (char));
            data.append(fileData).toHex();
        }
        ui->hexEdit->insertPlainText(data);
    }
    

    Produces the ASCII just fine:

    :1000000014C01BC01AC019C018C017C016C026C023
    :1000100014C013C012C011C010C00FC00EC00DC05C
    :100020000CC00BC00AC009C008C011241FBECFE518
    :10003000D1E0DEBFCDBF09D026C0E2CF12B883E346
    :1000400089B98AB188698AB90895F89480E886BD2B
    :1000500016BC7894F3DFB89AC098FFCF1F920F9226
    :100060000FB60F9211248F938CB1813619F082361E
    :1000700019F003C0C09A01C0C0988F910F900FBEB5
    :0A0080000F901F901895F894FFCF21
    :00000001FF
    

    That is not hex though. I am sure it is something very simple that I am missing, but...

    Any help would be appreciated, thanks.

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 15 Sept 2019, 06:39 last edited by
      #2

      @admkrk said in Displaying data in hex format:

      data.append(fileData).toHex();

      What do you think happens here? Take a look at the documentation: https://doc.qt.io/qt-5/qbytearray.html#toHex

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A Offline
        A Offline
        admkrk
        wrote on 15 Sept 2019, 15:43 last edited by
        #3

        OK, I am getting confused on the formats. What I was calling ASCII is the character. What I expected to happen is I would get the hex representation of it. In other words I expected to get something like:
        3A31303030...
        instead of:
        :1000...

        It looks like I am completely not understanding how I should go about this.

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 15 Sept 2019, 16:35 last edited by
          #4

          You did not took a look at the documentation I pointed you to - toHex() returns a new QByteArray, it does not modify the current object: "Returns a hex encoded copy of the byte array. "

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mpergand
            wrote on 15 Sept 2019, 17:11 last edited by mpergand
            #5

            It seems that what you want to do is some sort of HEX DUMP :
            Mididump.png

            Here my code:

            if(data[0] EQ (char)0xF0)
                           {
                           int add=0;      // address of the line
                           int rows=data.length()/16;  // number of lines
                           if(data.length()%16) rows++;
            
                           for(int i=0; i<rows; i++)
                               {
                               VariantList dump;
            
                               dump<<"";   // 
                               QByteArray line=data.mid(16*i,16);
                               QString hex,ascii=" ";
            
                               // 16 data by line
                               for(int b=0;b<line.length();b++)
                                   {
                                   uint8_t c=uint8_t(line.at(b));
                                   hex+=QString::asprintf("%02X ", c); // hex
            
                                   if( (c<32) OR (c>=127) ) c='.';
                                   ascii+=QString::asprintf("%c", c);  // ascii
                                   }
            
                               while(hex.size()<48) hex.append(' ');
                               dump<<QString::asprintf("%04X  ", add)+hex+ascii;  // address
                               item->addChildWithData(dump);
                               add+=16;
                               }
                           }
                       }
            

            As you can see i'm using asprintf to convert the data into the different formats. Very handy.

            1 Reply Last reply
            1
            • A Offline
              A Offline
              admkrk
              wrote on 15 Sept 2019, 23:41 last edited by
              #6

              @christian-ehrlicher said in Displaying data in hex format:

              You did not took a look at the documentation I pointed you to - toHex() returns a new QByteArray, it does not modify the current object: "Returns a hex encoded copy of the byte array. "

              I did look, but missed the part about a copy.

              @mpergand said in Displaying data in hex format:

              It seems that what you want to do is some sort of HEX DUMP :

              Yes, that is pretty much what I am looking do.

              Thank you both, if I can ever get a good nights sleep I should be able to figure something out.

              1 Reply Last reply
              1
              • A Offline
                A Offline
                admkrk
                wrote on 20 Sept 2019, 21:53 last edited by
                #7

                Amazing how some rest can clear your head. I discovered I needed to switch to reading a binary file and between the rest and looking at it from a different angle, it just came out right. I still have a long way to go, but at least the basic part is working now.

                void MainWindow::openHexFile(QString file)
                {
                    QFile f(file);
                    QByteArray data;
                    if(!f.open(QIODevice::ReadOnly))
                    {
                        qDebug() << "Error opening file.";
                        return;
                    }
                
                    while(!f.atEnd())
                    {
                        data = f.readLine().toHex();
                
                        for(int i = 0; i < data.size(); i++)
                        {
                            QChar temp = data.at(i);
                            ui->hexEdit->insertPlainText(temp);
                            if(i % 2)
                                ui->hexEdit->insertPlainText(" ");
                        }
                    }
                }
                

                Thanks again guys.

                1 Reply Last reply
                1

                1/7

                15 Sept 2019, 00:58

                • Login

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