Conversion of byte to binary



  • I am having difficulty with conversion of a byte held in a QByteArray to a binary value ussing QString::Number. I have a piece of code that copied 5 bytes from the serial device into a QByteArray. I then need to loop through the 5 bytes and process the individual bits in each byte. For debugging purposes I have a set up a TextBrowser object in the GUI and am displaying the byte data using 'ui->textBrowser->append()'.

    Here is my code:

    void Widget::rcvInstrStat(QByteArray data){
        // Receive and process instrument status data
    
        if (data.length() == 5) {  // have we received 5 bytes?
            if (isDebugModeOn) {
                ui->textBrowser->append("Status data received - " + QString::number(data.length()) + " bytes.");
               // Display bytes in debug window
                for (int i=0; i<data.length(); i++) {
                    ui->textBrowser->append("Byte: " + QString::number(data.at(i),2));
                }
                ui->textBrowser->append("Instrument status has changed.");
            }
    
            instrStat = data;  // save the new status
            updInstrStat(data);  // carry out update process
        }
    }
    

    What happens is a bit strange. It generally works OK with an output like:

    Status data received - 5 bytes.
    Byte: 100101
    Byte: 11111
    Byte: 0
    Byte: 0
    Byte: 101001
    Instrument status has changed.

    which allows me to see which bits are set. However, it would seem that for larger numbers (>128?) I get this:

    Status data received - 5 bytes.
    Byte: 1111111111111111111111111111111111111111111111111111111111000101
    Byte: 11111
    Byte: 0
    Byte: 0
    Byte: 101001
    Instrument status has changed.

    Obviously a byte should have 8 bits, so what's with the long string of 1s? If I set QString::Number to return the data in decimal, then it turns out that instead of getting what should be 197, I get -59. It so happens that 256-59 = 197 so it seems I am getting an inverse representation of the value in 64 bits. I tried substituting 'data.at(i)' with the literal number 197 and it then returned the string 11000101 as expected. Evidently the problem is with converting individual bytes from the QByteArray. Please note that I have also tried using data[i] instead of data.at(i), but it makes no difference.

    My code in updInstrStat() still seems to work OK and appears to be still responding correctly to specific bits being set, so it would appear that this is a display issue rather than a problem with the actual data being received although given the output being produced, it is difficult to say with full confidence.



  • Ok, with a bit more digging I think I have solved this myself. I am however wondering whether there is a neater way?

    void Widget::rcvInstrStat(QByteArray data){
        // Receive and process instrument status data
       unsigned char c;
    
        if (data.length() == 5) {  // have we received 5 bytes?
            if (isDebugModeOn) {
                ui->textBrowser->append("Status data received - " + QString::number(data.length()) + " bytes.");
               // Display bytes in debug window
                for (int i=0; i<data.length(); i++) {
                    c = data[i];
                    ui->textBrowser->append("Byte" + QString::number(i+1) + ": " + QString("%1").arg(c, 8, 2, QChar('0')));
                }
                ui->textBrowser->append("Instrument status has changed.");
            }
    
            instrStat = data;  // save the new status
            updInstrStat(data);  // carry out update process
        }
    }
    

    The output now includes padding and byte count to make it more readable:

    Byte1: 11000101
    Byte2: 00011111
    Byte3: 00000000
    Byte4: 00000000
    Byte5: 00101001

    However, if there is a better or more efficient way to accomplish this then I would be interested in some feedback.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.