Data Format issue



  • Hi,
    I'm not much perfect in c++ ,so I need help in solving the below issue.
    In my GUI , I have button and combo box which has values from 0 to 63.
    As I press button I need to send one byte whose format is W 3B 1A
    W and 1A are fixed constants.
    Where 3B is combined value of opcode(0011) & selected value from combo box(0 to 63)
    From the selected value , Lets say I choose 45 from 0 to 63,
    It's value in binary is 00101101 & out of 8 bits I must ignore 6&7 th bit as they will be always zero between 0 to 63,
    So left out is 6 bits 101101
    In my first byte I have to send bits from 5 to 2 I.e 1011 &
    along with it the opcode I.e 0011 , opcode must be appended before the 4 bits value
    So the complete byte is 00111011
    In order to get only Msb's , I split the above 6 bits & append the opcode.
    Thing is I get the binary form of data 00111011 but I must send in hex form I.e 3B
    I know how to convert binary to hex but in my case I get wrong conversions.
    This is my code

    int i1 = ui->comboBox_PS1 ->currentText().toFloat();
    i1 = 1 * i1;
    QString num1;
    num1.setNum(i1,2);//converting to binary
    QString NBYT1 = num1.mid(0,4); //splitting to get Msb's
    QString Data = QString::number(0)+QString::number(0)+QString::number(1)+QString::number(1)+ NBYT1; // opcode is 0011 and 1011 are Msb's
    QString BYT1 = "W" + sp + (Data) + sp + "1A";
    serial_1->write( BYT1.toStdString().c_str());
    if i do the above part ,i get
    W 00111011 1A
    This is correct but i want to send 00111011 in hex form, I.e 3B
    Please guide me if I'm going wrong anywhere.

    Regards
    Vidisha



  • @vidisha
    You don't need to convert numbers to strings and vice versa.
    Use shift function and work only with numbers:

    int Data = (3 << 4) | (i1 >> 2);
    

    3 is decimal value for 0011. Result from (3 << 4) is 00110000 binary or 48 decimal.
    If i1 = 45 = 101101, then (i1 >> 2) will give you 1011 = 11.
    Final result is 00111011 = 59.
    Then you can convert it to QString and choose format.
    For example:

    QString res = QString("W %1 1A"").arg(Data, 0, 16).toUpper();
    


  • @Stoyan
    Thankyou so much...
    This is what i needed..
    Really tq so much..



  • @Stoyan
    One more thing is i have to write 2nd Byte which is 2 LSB's of i1 & value from ( 31.5 to 0).

    Im poor in Bit masking,
    How to get only 2 LSB's of i1 i.e 45(101101)



  • @vidisha

    int Data2 = i1 & 3;
    


  • @Stoyan
    Ya got it,but i have to OR it with value chosen from ( 31.5 to 0)
    which has floating point numbers too..

    int i1 = ui->comboBox_PS1 ->currentText().toFloat();
    float AT1 = ui-> comboBox_AT1 ->currentText().toFloat();
    int Byt2 = (i1 & 3 );
    int BYT3 = Byt2 << 6;
    int BYT4 = (BYT3 | (AT1));

    it gives me error
    error: C2297: '|' : illegal, right operand has type 'float'

    so what's the correct way to convert floating numbers to add with decimal number



  • @vidisha
    You can't use bit operations on float or double, because they have completely different inner representations. But if a float number have one digit after the point you can multiply it by 10 and then convert result to integer. With this number you can use bit operations, but result may be different of what you expect.
    I don't understand in this case what you want to achieve. Can you give me expected result for a sample input values?



  • @Stoyan
    Hi,
    I'm doing typecasting I.e converting to char and performing logical operations on it.
    This is solved for me.
    Tq so much.😊
    One more thing I want to know is
    I get 2 bytes incoming over serial port.
    So, I need to compare each bit with a reference bit.
    So how can I access each bit of a byte?



  • @vidisha
    Comparison with one bit:

    int bit0 = 1;  // BIT 0 - 00000001
    int bit1 = 2;  // BIT 1 - 00000010
    ...
    int bit7 = 128;  // BIT 7 - 10000000
    ...
    if (num & bit0) 
        return true;  // BIT 0 in num is 1
    else
        return false;
    


  • @Stoyan
    Hi ,
    Actually i send 2 bytes(commands) & in response i get 2 bytes.
    I want to check whether received each bit is set or not.
    I dont know what each bit should be, but i want to see whether it is 1 or 0.
    so for reference i defined -> unsigned short tocheck = 9203( 00100011 11110011)
    & i compare each received bit with reference bits.

    unsigned short tocheck = 9203;
    for(int bitnum = 0; bitnum <= 16; bitnum++){
    bool biton = (tocheck >> bitnum) & 1;
    qDebug() << "bit" << bitnum << "->" << biton;

        }
    

    Is it correct the way i'm doing?



  • @vidisha
    It is correct. But if you want to use them afterward individually, may be it is better to store them in QBitArray. It depends on what you want to do with them.



  • @Stoyan
    Hi,
    It;s quite not related to above post but i need to solve it urgently so,
    I receive 10 Bytes over serial port in hex form,
    4F C0 6F C0....AF C0
    My incoming data is in m_readData.
    QByteArray buf ;
    buf += serial_1->readAll();
    m_readData.append(buf);

    I have to perform bitwise operations on the received data, so i need to convert it into int or binary form,
    so , how can i convert incoming hex data to some other form??

    Regards
    Vidisha


  • Moderators

    @vidisha Well, you could read documentation: http://doc.qt.io/qt-5/qbytearray.html#fromHex


Log in to reply
 

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