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. Data Format issue
Qt 6.11 is out! See what's new in the release blog

Data Format issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 5.7k Views 1 Watching
  • 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.
  • V vidisha

    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

    S Offline
    S Offline
    Stoyan
    wrote on last edited by Stoyan
    #2

    @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();
    
    V 2 Replies Last reply
    5
    • S Stoyan

      @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();
      
      V Offline
      V Offline
      vidisha
      wrote on last edited by
      #3

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

      1 Reply Last reply
      1
      • S Stoyan

        @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();
        
        V Offline
        V Offline
        vidisha
        wrote on last edited by
        #4

        @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)

        S 1 Reply Last reply
        0
        • V vidisha

          @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)

          S Offline
          S Offline
          Stoyan
          wrote on last edited by
          #5

          @vidisha

          int Data2 = i1 & 3;
          
          V 1 Reply Last reply
          0
          • S Stoyan

            @vidisha

            int Data2 = i1 & 3;
            
            V Offline
            V Offline
            vidisha
            wrote on last edited by
            #6

            @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

            S 1 Reply Last reply
            0
            • V vidisha

              @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

              S Offline
              S Offline
              Stoyan
              wrote on last edited by
              #7

              @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?

              V 1 Reply Last reply
              0
              • S Stoyan

                @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?

                V Offline
                V Offline
                vidisha
                wrote on last edited by
                #8

                @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?

                S 1 Reply Last reply
                1
                • V vidisha

                  @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?

                  S Offline
                  S Offline
                  Stoyan
                  wrote on last edited by Stoyan
                  #9

                  @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;
                  
                  V 2 Replies Last reply
                  1
                  • S Stoyan

                    @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;
                    
                    V Offline
                    V Offline
                    vidisha
                    wrote on last edited by
                    #10

                    @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?

                    S 1 Reply Last reply
                    0
                    • V vidisha

                      @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?

                      S Offline
                      S Offline
                      Stoyan
                      wrote on last edited by
                      #11

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

                      1 Reply Last reply
                      0
                      • S Stoyan

                        @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;
                        
                        V Offline
                        V Offline
                        vidisha
                        wrote on last edited by
                        #12

                        @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

                        jsulmJ 1 Reply Last reply
                        0
                        • V vidisha

                          @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

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

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

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          • Login

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