Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • 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 7.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.
  • 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
                      • Unsolved