Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Displaying data in hex format



  • 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.


  • Qt Champions 2019

    @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



  • 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.


  • Qt Champions 2019

    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. "



  • 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.



  • @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.



  • 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.


Log in to reply