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 problem
Qt 6.11 is out! See what's new in the release blog

QByteArray problem

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 6.9k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #6

    Yes, the if you use [] that casts it automatiocally to signed but the value is still 0xff

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    1
    • Z Zizione

      @aha_1980
      There is my full code , this is just test unit. I need exactly int value in it , and how i understand '\xFF' will store char in it ?

      #include <QCoreApplication>
      #include <QByteArray>
      #include <QString>
      #include <QTextStream>
      int main(int argc, char *argv[])
      {
          QTextStream cin(stdin); cin.setCodec("CP866");
          QTextStream cout(stdout); cout.setCodec("CP866");
          QCoreApplication a(argc, argv);
          QByteArray ba;
          ba.resize(4);
          ba[0] = 0xFF;
          ba[1] = 0xFF;
          ba[2] = 0xF;
          ba[3] = 0xF;
          char tdi[16];
          int b=0;
          for(int i=0;i<4;i++)
          {
              uint16_t mask = 0x8;
              for (int j = 0; j < 4; j++)
              {
                  tdi[b] = (ba[i] & mask) ? '1' : '0';
                  mask >>= 1;
                  b++;
              }
      
          }
          for(int i=0;i<16;i++)
          {
              cout<<tdi[i]<<endl;
          }
          cout<<tdi<<endl;
          cout<<ba[0]<<endl;
          //cout<<sizeof(tdi)<<endl;
          return a.exec();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #7

      @Zizione
      In addition to the solutions offered by @VRonin....

      I am not a C++-er, but I believe in your case your can use the U suffix to make your constant values unsigned int and thus convertible to unsigned char:

      ba[0] = 0xFFU;
      

      ?

      J.HilkJ aha_1980A 2 Replies Last reply
      1
      • JonBJ JonB

        @Zizione
        In addition to the solutions offered by @VRonin....

        I am not a C++-er, but I believe in your case your can use the U suffix to make your constant values unsigned int and thus convertible to unsigned char:

        ba[0] = 0xFFU;
        

        ?

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

        @JonB
        Wow, that totally slipped my mind, 👍


        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
        • JonBJ JonB

          @Zizione
          In addition to the solutions offered by @VRonin....

          I am not a C++-er, but I believe in your case your can use the U suffix to make your constant values unsigned int and thus convertible to unsigned char:

          ba[0] = 0xFFU;
          

          ?

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @JonB said in QByteArray problem:

          @Zizione
          In addition to the solutions offered by @VRonin....

          I am not a C++-er, but I believe in your case your can use the U suffix to make your constant values unsigned int and thus convertible to unsigned char:

          ba[0] = 0xFFU;
          

          ?

          Yeah, but ba is signed char...

          Qt has to stay free or it will die.

          J.HilkJ 1 Reply Last reply
          0
          • aha_1980A aha_1980

            @JonB said in QByteArray problem:

            @Zizione
            In addition to the solutions offered by @VRonin....

            I am not a C++-er, but I believe in your case your can use the U suffix to make your constant values unsigned int and thus convertible to unsigned char:

            ba[0] = 0xFFU;
            

            ?

            Yeah, but ba is signed char...

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

            @aha_1980
            true, but the warning one gets with ba[0] = 0xFF is not about usigned and signed char but rather between int and char

            0xFFU == unsigned char(0xFF) == uchar(0xFF) != 0xFF

            the implicit conversation to ba[0] does not "drop" information


            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.

            aha_1980A 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @aha_1980
              true, but the warning one gets with ba[0] = 0xFF is not about usigned and signed char but rather between int and char

              0xFFU == unsigned char(0xFF) == uchar(0xFF) != 0xFF

              the implicit conversation to ba[0] does not "drop" information

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by aha_1980
              #11

              @J.Hilk have you tried?

                  QByteArray ba;
                  ba.resize(4);
                  ba[0] = char(0xFF); // no warning
                  ba[1] = 0xFFU; // warning: implicit conversion changes signedness: 'unsigned int' to 'char'
                  ba[2] = 0xFF; // warning: implicit conversion changes signedness: 'int' to 'char'
              

              Edit: screenshot

              Qt has to stay free or it will die.

              J.HilkJ 1 Reply Last reply
              1
              • aha_1980A aha_1980

                @J.Hilk have you tried?

                    QByteArray ba;
                    ba.resize(4);
                    ba[0] = char(0xFF); // no warning
                    ba[1] = 0xFFU; // warning: implicit conversion changes signedness: 'unsigned int' to 'char'
                    ba[2] = 0xFF; // warning: implicit conversion changes signedness: 'int' to 'char'
                

                Edit: screenshot

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

                @aha_1980 said in QByteArray problem:

                @J.Hilk have you tried?

                I always do, well 95%, before I post ;-)

                0_1523616653797_75eb60ce-4142-42c5-9e3b-9507738d3e70-image.png

                to be honest, I'm surprised about the uint part.


                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.

                aha_1980A 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @aha_1980 said in QByteArray problem:

                  @J.Hilk have you tried?

                  I always do, well 95%, before I post ;-)

                  0_1523616653797_75eb60ce-4142-42c5-9e3b-9507738d3e70-image.png

                  to be honest, I'm surprised about the uint part.

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  @J.Hilk

                  then you can be surprised about the uchar() part too.

                  But the biggest surprise: your code will not even compile in gcc and clang because of line 65.

                  And one more hint: your code will crash as you access invalid index in ba ;)

                  Qt has to stay free or it will die.

                  J.HilkJ 1 Reply Last reply
                  0
                  • aha_1980A aha_1980

                    @J.Hilk

                    then you can be surprised about the uchar() part too.

                    But the biggest surprise: your code will not even compile in gcc and clang because of line 65.

                    And one more hint: your code will crash as you access invalid index in ba ;)

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

                    @aha_1980 said in QByteArray problem:

                    @J.Hilk
                    But the biggest surprise: your code will not even compile in gcc and clang because of line 65.

                    I saw you using linux in the screen shot and testet that there than, and noticed it too

                    And one more hint: your code will crash as you access invalid index in ba ;)

                    nope doesn't crash, ignore the project name its my test anything project x).
                    0_1523617980226_bd815aeb-ebbc-48dc-8550-62179d940261-image.png !

                    resize is apparently not a necessity?

                    Edit:
                    also ignore value 0, I commeted that line out, to run it with gcc, so theres a random value.
                    // ba[0] = unsigned char(0xFF);


                    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.

                    aha_1980A 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @aha_1980 said in QByteArray problem:

                      @J.Hilk
                      But the biggest surprise: your code will not even compile in gcc and clang because of line 65.

                      I saw you using linux in the screen shot and testet that there than, and noticed it too

                      And one more hint: your code will crash as you access invalid index in ba ;)

                      nope doesn't crash, ignore the project name its my test anything project x).
                      0_1523617980226_bd815aeb-ebbc-48dc-8550-62179d940261-image.png !

                      resize is apparently not a necessity?

                      Edit:
                      also ignore value 0, I commeted that line out, to run it with gcc, so theres a random value.
                      // ba[0] = unsigned char(0xFF);

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @J.Hilk said in QByteArray problem:

                      resize is apparently not a necessity?

                      Ah, my fault: "If an assignment is made beyond the end of the byte array, the array is extended with resize() before the assignment takes place."

                      A big difference to QVector or QList here.

                      Qt has to stay free or it will die.

                      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