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 range issue?
QtWS25 Last Chance

QByteArray range issue?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 900 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.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by
    #1

    Dear all,

    i have:

    void LED::setColor(int red, int green, int blue)
    {
        QByteArray writeData;
        writeData.resize(15);
    
        // red
        int red_checksum = 256 - 5 - 97 - 16 - red;
        if (red_checksum < 0) { red_checksum = red_checksum + 256; }
        writeData[0] = 5;
        writeData[1] = 97;
        writeData[2] = 16;
        writeData[3] = red;
        writeData[4] = red_checksum;
    
        // green
        int green_checksum = 256 - 5 - 97 - 17 - green;
        if (green_checksum < 0) { green_checksum = green_checksum + 256; }
        writeData[5] = 5;
        writeData[6] = 97;
        writeData[7] = 17;
        writeData[8] = green;
        writeData[9] = green_checksum;
    
        // blue
        int blue_checksum = 256 - 5 - 97 - 18 - blue;
        if (blue_checksum < 0) { blue_checksum = blue_checksum + 256; }
        writeData[10] = 5;
        writeData[11] = 97;
        writeData[12] = 18;
        writeData[13] = blue;
        writeData[14] = blue_checksum;
    
        qDebug() << "Red" << QByteArray::number(writeData[0])<< QByteArray::number(writeData[1]) << QByteArray::number(writeData[2]) << QByteArray::number(writeData[3]) << QByteArray::number(writeData[4]);
        qDebug() << "Blue" << QByteArray::number(writeData[5]) << QByteArray::number(writeData[6]) << QByteArray::number(writeData[7]) << QByteArray::number(writeData[8]) << QByteArray::number(writeData[9]);
        qDebug() << "Green" << QByteArray::number(writeData[10]) << QByteArray::number(writeData[11]) << QByteArray::number(writeData[12]) << QByteArray::number(writeData[13]) << QByteArray::number(writeData[14]);
    }
    

    and when calling setColor(255, 0, 0), the outcome of writeData[3] as -1 instead of 255.

    Also, same for calculation of red_checksum, green_checksum and blue_checksum,
    the result is:

    red_checksum as -117
    green_checksum as -119
    blue_checksum as -120
    code_text
    

    but realy it should be:

    red_checksum = 256 - 5 - 97 - 16 - red = 256 - 5 - 97 - 16 - 255 = -117; if(red_checksum<0) {red_checksum=red_checksum+256} which means red_checksum= -117 + 256 = 139
    

    same for other colors...

    so is this range issue? is QByteArray capable only of 254chars or what is the problem here?

    jsulmJ 1 Reply Last reply
    0
    • S shokarta

      Dear all,

      i have:

      void LED::setColor(int red, int green, int blue)
      {
          QByteArray writeData;
          writeData.resize(15);
      
          // red
          int red_checksum = 256 - 5 - 97 - 16 - red;
          if (red_checksum < 0) { red_checksum = red_checksum + 256; }
          writeData[0] = 5;
          writeData[1] = 97;
          writeData[2] = 16;
          writeData[3] = red;
          writeData[4] = red_checksum;
      
          // green
          int green_checksum = 256 - 5 - 97 - 17 - green;
          if (green_checksum < 0) { green_checksum = green_checksum + 256; }
          writeData[5] = 5;
          writeData[6] = 97;
          writeData[7] = 17;
          writeData[8] = green;
          writeData[9] = green_checksum;
      
          // blue
          int blue_checksum = 256 - 5 - 97 - 18 - blue;
          if (blue_checksum < 0) { blue_checksum = blue_checksum + 256; }
          writeData[10] = 5;
          writeData[11] = 97;
          writeData[12] = 18;
          writeData[13] = blue;
          writeData[14] = blue_checksum;
      
          qDebug() << "Red" << QByteArray::number(writeData[0])<< QByteArray::number(writeData[1]) << QByteArray::number(writeData[2]) << QByteArray::number(writeData[3]) << QByteArray::number(writeData[4]);
          qDebug() << "Blue" << QByteArray::number(writeData[5]) << QByteArray::number(writeData[6]) << QByteArray::number(writeData[7]) << QByteArray::number(writeData[8]) << QByteArray::number(writeData[9]);
          qDebug() << "Green" << QByteArray::number(writeData[10]) << QByteArray::number(writeData[11]) << QByteArray::number(writeData[12]) << QByteArray::number(writeData[13]) << QByteArray::number(writeData[14]);
      }
      

      and when calling setColor(255, 0, 0), the outcome of writeData[3] as -1 instead of 255.

      Also, same for calculation of red_checksum, green_checksum and blue_checksum,
      the result is:

      red_checksum as -117
      green_checksum as -119
      blue_checksum as -120
      code_text
      

      but realy it should be:

      red_checksum = 256 - 5 - 97 - 16 - red = 256 - 5 - 97 - 16 - 255 = -117; if(red_checksum<0) {red_checksum=red_checksum+256} which means red_checksum= -117 + 256 = 139
      

      same for other colors...

      so is this range issue? is QByteArray capable only of 254chars or what is the problem here?

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

      @shokarta said in QByteArray range issue?:

      the outcome of writeData[3] as -1 instead of 255

      -1 and 255 have exact same binary representation:
      255: b11111111
      -1 (two complement): b00000001 -> (invert bits) b11111110 -> (add 1) b11111111
      See https://en.wikipedia.org/wiki/Two's_complement
      You need to interpret writeData[3] as unsigned char to see 255.

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

      1 Reply Last reply
      4
      • S Offline
        S Offline
        shokarta
        wrote on last edited by
        #3

        Thanks, but i guess the Binary is not the same for -117 and 139.
        Basicaly I need to write the symbol which is behind DEC number…
        so from ASCII table i need to insert the char from column "Symbol" which is behind the DEC number.
        And it needs to be inserted into QByteArray…
        Can I kindly ask you to show me an exmaple how?

        jsulmJ JonBJ 2 Replies Last reply
        0
        • S shokarta

          Thanks, but i guess the Binary is not the same for -117 and 139.
          Basicaly I need to write the symbol which is behind DEC number…
          so from ASCII table i need to insert the char from column "Symbol" which is behind the DEC number.
          And it needs to be inserted into QByteArray…
          Can I kindly ask you to show me an exmaple how?

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

          @shokarta There is https://doc.qt.io/qt-5/qchar.html#fromLatin1 and https://doc.qt.io/qt-5/qchar.html#toLatin1 to convert from ASCII

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

          1 Reply Last reply
          0
          • S shokarta

            Thanks, but i guess the Binary is not the same for -117 and 139.
            Basicaly I need to write the symbol which is behind DEC number…
            so from ASCII table i need to insert the char from column "Symbol" which is behind the DEC number.
            And it needs to be inserted into QByteArray…
            Can I kindly ask you to show me an exmaple how?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @shokarta said in QByteArray range issue?:

            Thanks, but i guess the Binary is not the same for -117 and 139.

            ? 117 + 139 == 256, so they are the same.

            S 1 Reply Last reply
            0
            • JonBJ JonB

              @shokarta said in QByteArray range issue?:

              Thanks, but i guess the Binary is not the same for -117 and 139.

              ? 117 + 139 == 256, so they are the same.

              S Offline
              S Offline
              shokarta
              wrote on last edited by
              #6

              @JonB said in QByteArray range issue?:

              @shokarta said in QByteArray range issue?:

              Thanks, but i guess the Binary is not the same for -117 and 139.

              ? 117 + 139 == 256, so they are the same.

              no,
              im assigning via writeData[3] = 255;
              then trying to see what is behind by:
              qDebug() << QByteArray::number(writeData[3]) << static_cast<char>(writeData[3]);
              so instead of having:
              255 ÿ
              im having:
              -1 ?

              and same goes for the:
              red_checksum = 256 - 5 - 97 - 16 - red //256 - 5 - 97 - 16 - 255 = -117
              if(red_checksum<0) {red_checksum=red_checksum+256} //which means red_checksum= -117 + 256 = 139
              writeData[4] = red_checksum;
              and then printing:
              qDebug() << QByteArray::number(writeData[4]) << static_cast<char>(writeData[4]);
              I expect to have:
              139 ‹
              but im having:
              -117 y

              J.HilkJ 1 Reply Last reply
              0
              • S shokarta

                @JonB said in QByteArray range issue?:

                @shokarta said in QByteArray range issue?:

                Thanks, but i guess the Binary is not the same for -117 and 139.

                ? 117 + 139 == 256, so they are the same.

                no,
                im assigning via writeData[3] = 255;
                then trying to see what is behind by:
                qDebug() << QByteArray::number(writeData[3]) << static_cast<char>(writeData[3]);
                so instead of having:
                255 ÿ
                im having:
                -1 ?

                and same goes for the:
                red_checksum = 256 - 5 - 97 - 16 - red //256 - 5 - 97 - 16 - 255 = -117
                if(red_checksum<0) {red_checksum=red_checksum+256} //which means red_checksum= -117 + 256 = 139
                writeData[4] = red_checksum;
                and then printing:
                qDebug() << QByteArray::number(writeData[4]) << static_cast<char>(writeData[4]);
                I expect to have:
                139 ‹
                but im having:
                -117 y

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @shokarta No.
                @JonB is absolutely correct.

                If you want to check against the upper range of unsigned, cast it properly
                qDebug() << QByteArray::number(writeData[3]) << static_cast<char>(writeData[3]); qDebug() << QByteArray::number(writeData[3]) << static_cast<unsigned char>(writeData[3]);

                qDebug () << static_cast <unsigned char> (-117) << static_cast <unsigned char> (139) << ( static_cast <unsigned char> (-117) == static_cast <unsigned char> (139));

                -> 139 139 true


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  stretchthebits
                  wrote on last edited by
                  #8

                  QByteArray seems to be just an array of char, which are signed values.
                  In other words, the range is from -128 to 127.
                  In binary format, the last bit is for the sign. 0 is for positive values and 1 is for negative values.
                  Numbers from 0 to 127 are mapped to 0000 0000 to 0111 1111.
                  0111 1111 = 127 <==== notice the last bit is 0. 127 is a positive value.
                  Numbers from -128 to -1 are mapped to 1000 0000 to 1111 1111.

                  127+1 would cause an overflow. You get 1000 0000 which is actually -128.

                  It sounds like you want to work in the range of -255 to 255 or maybe a wider range.
                  In that case, you would have to use a short (16 bit signed) or int (32 bit signed) array.
                  I don't know if Qt has such a thing.
                  You can use STL arrays or write your own classes.

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @stretchthebits said in QByteArray range issue?:

                    It sounds like you want to work in the range of -255 to 255

                    No, he simply don't understand the difference between a signed and an unsigned char data type.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    S 1 Reply Last reply
                    2
                    • S stretchthebits

                      QByteArray seems to be just an array of char, which are signed values.
                      In other words, the range is from -128 to 127.
                      In binary format, the last bit is for the sign. 0 is for positive values and 1 is for negative values.
                      Numbers from 0 to 127 are mapped to 0000 0000 to 0111 1111.
                      0111 1111 = 127 <==== notice the last bit is 0. 127 is a positive value.
                      Numbers from -128 to -1 are mapped to 1000 0000 to 1111 1111.

                      127+1 would cause an overflow. You get 1000 0000 which is actually -128.

                      It sounds like you want to work in the range of -255 to 255 or maybe a wider range.
                      In that case, you would have to use a short (16 bit signed) or int (32 bit signed) array.
                      I don't know if Qt has such a thing.
                      You can use STL arrays or write your own classes.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @stretchthebits said in QByteArray range issue?:

                        It sounds like you want to work in the range of -255 to 255

                        No, he simply don't understand the difference between a signed and an unsigned char data type.

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

                        @Christian-Ehrlicher
                        That's ok, that is why I explained to him how it works for the case of char. It is up to him if he wants short or int. He seems to be working with colors and using 8 bit but char is not appropriate for what he is doing.

                        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