Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QByteArray increment, and qDebug() benaviour

    General and Desktop
    3
    10
    2149
    Loading More Posts
    • 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.
    • McLion
      McLion last edited by 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-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @McLion last edited by

        @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

        McLion 2 Replies Last reply Reply Quote 3
        • McLion
          McLion @raven-worx last edited by

          @raven-worx

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

          works.

          1 Reply Last reply Reply Quote 0
          • McLion
            McLion @raven-worx last edited by

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

            raven-worx 1 Reply Last reply Reply Quote 0
            • raven-worx
              raven-worx Moderators @McLion last edited by

              @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 Reply Quote 3
              • McLion
                McLion last edited by

                @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-worx JKSH 2 Replies Last reply Reply Quote 0
                • raven-worx
                  raven-worx Moderators @McLion last edited by

                  @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

                  McLion 1 Reply Last reply Reply Quote 0
                  • McLion
                    McLion @raven-worx last edited by

                    @raven-worx

                    Didn't help ... never mind.

                    1 Reply Last reply Reply Quote 0
                    • JKSH
                      JKSH Moderators @McLion last edited by 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".

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

                      McLion 1 Reply Last reply Reply Quote 5
                      • McLion
                        McLion @JKSH last edited by

                        @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 Reply Quote 0
                        • First post
                          Last post