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. QByteArray increment, and qDebug() benaviour
Forum Updated to NodeBB v4.3 + New Features

QByteArray increment, and qDebug() benaviour

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.6k Views 3 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.
  • McLionM Offline
    McLionM Offline
    McLion
    wrote on last edited by McLion
    #1

    Hi

    A very basic one .. I just can't find it ;-)

    Is it just me that is missing something or is it not possible to increment in a QByteArray?

    qbTest[iIdx]+=1;
    

    If possible, how?
    Or do I better use a standard C++ array?

    Something else I just can't find why:
    If qbTest[iIdx] is = 1, qDebug() does output nothing - which is OK I assume because 0x01 is 'not visible'.
    If I add '0' it should be 0x31 which is '1', but it outputs 49 - I regularly use this in C without issues.
    If I do +'0'-48 - which actually seems very stupid, does it - it shows a correct '1'
    Can someone shed some light on this?

    Thanks

    raven-worxR 1 Reply Last reply
    0
    • McLionM McLion

      Hi

      A very basic one .. I just can't find it ;-)

      Is it just me that is missing something or is it not possible to increment in a QByteArray?

      qbTest[iIdx]+=1;
      

      If possible, how?
      Or do I better use a standard C++ array?

      Something else I just can't find why:
      If qbTest[iIdx] is = 1, qDebug() does output nothing - which is OK I assume because 0x01 is 'not visible'.
      If I add '0' it should be 0x31 which is '1', but it outputs 49 - I regularly use this in C without issues.
      If I do +'0'-48 - which actually seems very stupid, does it - it shows a correct '1'
      Can someone shed some light on this?

      Thanks

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @McLion said in QByteArray increment:

      Is it just me that is missing something or is it not possible to increment in a QByteArray?

      i think you are just assigning your value to a copy and thus it gets lost.

      Does the following work?

      qbTest[iIdx] = char(qbTest[iIdx]) + 1;
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      McLionM 2 Replies Last reply
      3
      • raven-worxR raven-worx

        @McLion said in QByteArray increment:

        Is it just me that is missing something or is it not possible to increment in a QByteArray?

        i think you are just assigning your value to a copy and thus it gets lost.

        Does the following work?

        qbTest[iIdx] = char(qbTest[iIdx]) + 1;
        
        McLionM Offline
        McLionM Offline
        McLion
        wrote on last edited by
        #3

        @raven-worx

        qbTest[iIdx] = qbTest[iIdx] + 1;
        

        works.

        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @McLion said in QByteArray increment:

          Is it just me that is missing something or is it not possible to increment in a QByteArray?

          i think you are just assigning your value to a copy and thus it gets lost.

          Does the following work?

          qbTest[iIdx] = char(qbTest[iIdx]) + 1;
          
          McLionM Offline
          McLionM Offline
          McLion
          wrote on last edited by
          #4

          @raven-worx
          += as well as ++ returns a 'no match for operator...'

          raven-worxR 1 Reply Last reply
          0
          • McLionM McLion

            @raven-worx
            += as well as ++ returns a 'no match for operator...'

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @McLion
            QByteArray's [] operator returns a QByteRef (which is unfortunately not documented, but available via public API)
            Honestly i am not quite sure why the + even works, since the QByteRef class hasn't defined it. But i guess the compiler implicitly converts it to char maybe

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            3
            • McLionM Offline
              McLionM Offline
              McLion
              wrote on last edited by
              #6

              @raven-worx
              Thanks. I implemented it the (longer) way it works.

              Anybody an idea about the Debug() question?
              Sorry I put 2 issues in one thread ;-)

              raven-worxR JKSHJ 2 Replies Last reply
              0
              • McLionM McLion

                @raven-worx
                Thanks. I implemented it the (longer) way it works.

                Anybody an idea about the Debug() question?
                Sorry I put 2 issues in one thread ;-)

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                @McLion said in QByteArray increment, and qDebug() benaviour:

                Anybody an idea about the Debug() question?

                try passing char/char* to qDebug(), instead of the Qt classes.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                McLionM 1 Reply Last reply
                0
                • raven-worxR raven-worx

                  @McLion said in QByteArray increment, and qDebug() benaviour:

                  Anybody an idea about the Debug() question?

                  try passing char/char* to qDebug(), instead of the Qt classes.

                  McLionM Offline
                  McLionM Offline
                  McLion
                  wrote on last edited by
                  #8

                  @raven-worx

                  Didn't help ... never mind.

                  1 Reply Last reply
                  0
                  • McLionM McLion

                    @raven-worx
                    Thanks. I implemented it the (longer) way it works.

                    Anybody an idea about the Debug() question?
                    Sorry I put 2 issues in one thread ;-)

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by JKSH
                    #9

                    @McLion said in QByteArray increment, and qDebug() benaviour:

                    I regularly use this in C without issues.

                    How do you print it in C? Note:

                    • printf("%d\n", '1') prints "49"
                    • printf("%c\n", 49) prints "1"

                    Similarly, QDebug formats char values using %c and formats int values using %d.

                    Note also, when you add/subtract char values, the compiler (at least MSVC 2013 and GCC 4.8) implicitly casts them to int. Proof:

                    // Input:
                    #include <typeinfo>
                    ...
                    qDebug() << typeid('0' + 1).name();
                    qDebug() << typeid('0' - '0').name();
                    
                    // Output (on MSVC 2013 and GCC 4.8):
                    int
                    int
                    

                    If I add '0' it should be 0x31 which is '1', but it outputs 49

                    QDebug receives (int)0x31, so it prints "49".

                    If I do +'0'-48 - which actually seems very stupid, does it - it shows a correct '1'

                    Your code is 1 + '0' - 48 == 1 + 48 - 48 == 1.

                    QDebug receives a (int)0x01, so it prints "1".

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    McLionM 1 Reply Last reply
                    5
                    • JKSHJ JKSH

                      @McLion said in QByteArray increment, and qDebug() benaviour:

                      I regularly use this in C without issues.

                      How do you print it in C? Note:

                      • printf("%d\n", '1') prints "49"
                      • printf("%c\n", 49) prints "1"

                      Similarly, QDebug formats char values using %c and formats int values using %d.

                      Note also, when you add/subtract char values, the compiler (at least MSVC 2013 and GCC 4.8) implicitly casts them to int. Proof:

                      // Input:
                      #include <typeinfo>
                      ...
                      qDebug() << typeid('0' + 1).name();
                      qDebug() << typeid('0' - '0').name();
                      
                      // Output (on MSVC 2013 and GCC 4.8):
                      int
                      int
                      

                      If I add '0' it should be 0x31 which is '1', but it outputs 49

                      QDebug receives (int)0x31, so it prints "49".

                      If I do +'0'-48 - which actually seems very stupid, does it - it shows a correct '1'

                      Your code is 1 + '0' - 48 == 1 + 48 - 48 == 1.

                      QDebug receives a (int)0x01, so it prints "1".

                      McLionM Offline
                      McLionM Offline
                      McLion
                      wrote on last edited by
                      #10

                      @JKSH
                      Thanks a lot for this detailed explanation ... which explains a lot.
                      The value in question is from a QByteArray and therefore is a char.
                      When casting to int instead of doing the stupid +'0'-48, everything works as expected.

                      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