QByteArray sum element and Converting into integer



  • Hi, I'm new beginner in Qt. I'm using QbyteArray to read int 2 byte data from Serialport. I need to sum element
    such as Serialport sent { 0xff, 0x0f } and QbyteArray data received { data[0] = 0xff , data[1] = 0x0f }
    I need to data[1] brfore data[0] ( 0000 1111 , 1111 1111) --> to sum --> (0000 1111 + 1111 1111) = ( 0000 1111 1111 1111)
    and converting to integer.

    coding

    QByteArray data = serial.readAll();

    int num = data[0].prepend(data[1]);

    ui->lcdnumber->display(num);

    but it's doesn't work. Thanks


  • Moderators

    Hi,
    generally speaking, you need to take a look at bitwise operations in C++. Your problem boils down to:

    quint8 a = 0xff;
    quint8 b = 0x0f;
    quint16 result = (quint16)b<<8 | (quint16)a;
    

    Hope this helps!



  • It's work. Thank you so much


Log in to reply
 

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