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. How can I assign a byte value to a specific index of a bytearray without causing its size to change??
QtWS25 Last Chance

How can I assign a byte value to a specific index of a bytearray without causing its size to change??

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.2k Views
  • 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.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    this seems like a no brainer but I can´t for the life of me figure this out...

    I tried...
    @
    TX_Buffer.replace(sizeof(TX_Msg), 1, &((DI.mid(2, 2).toInt(&ok, 16) + 0x33) % 256));
    @

    but get and error :
    error: lvalue required as unary ‘&’ operand

    1 Reply Last reply
    0
    • D Offline
      D Offline
      danilocesar
      wrote on last edited by
      #2

      Try this:
      @QByteArray ba("Hello world");
      char *data = ba.data();
      ba.data()[1] = 'B';
      qWarning() << ba; //shows "HBllo world"
      @

      size() always returns 11.

      <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
      Software Engineer

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        Your third argument

        @&((DI.mid(2, 2).toInt(&ok, 16) + 0x33) % 256)@

        to replace is not an C++ object (which you can get an address with &), but an integer.

        Danilo:
        @ba[1] = 'B';@
        should do the trick even shorter

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • D Offline
          D Offline
          danilocesar
          wrote on last edited by
          #4

          Outch, my bad... You're right.

          <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
          Software Engineer

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            No problem - it was a long and hard week, so even the cutest brain gets exhausted sometimes :-)

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • P Offline
              P Offline
              phamtv
              wrote on last edited by
              #6

              thanks guys!

              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