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

QByteArray problem

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 4.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.
  • Z Offline
    Z Offline
    Zizione
    wrote on last edited by
    #1

    Hi guys. I am new in QT. I started developing a programm which working with bytes, and when I tried to use a QbyteArray, I've got some problem. There is a line in QByteArray class reference :
    QByteArray ba;
    ba.resize(5);
    ba[0] = 0x3c;
    ba[1] = 0xb8;
    ba[2] = 0x64;
    ba[3] = 0x18;
    ba[4] = 0xca;
    QByteArray must store 1 byte in each cell of it, but when i trying to do the same it gives me a warning:
    QByteArray ba;
    ba.resize(4);
    ba[0] = 0xFF;
    Result of the output ba[0] is symbol "?". It means that it stored not 8 bit but 6, symbol "?" is 63 in ascii table and 00111111 in binary code. Warning is C4309. How can i store all 8 bits in QByteArray ? (P.S. sorry for my awful English)

    aha_1980A 1 Reply Last reply
    0
    • Z Zizione

      Hi guys. I am new in QT. I started developing a programm which working with bytes, and when I tried to use a QbyteArray, I've got some problem. There is a line in QByteArray class reference :
      QByteArray ba;
      ba.resize(5);
      ba[0] = 0x3c;
      ba[1] = 0xb8;
      ba[2] = 0x64;
      ba[3] = 0x18;
      ba[4] = 0xca;
      QByteArray must store 1 byte in each cell of it, but when i trying to do the same it gives me a warning:
      QByteArray ba;
      ba.resize(4);
      ba[0] = 0xFF;
      Result of the output ba[0] is symbol "?". It means that it stored not 8 bit but 6, symbol "?" is 63 in ascii table and 00111111 in binary code. Warning is C4309. How can i store all 8 bits in QByteArray ? (P.S. sorry for my awful English)

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

      Hi @Zizione:

      QByteArray ba;
      ba.resize(4);
      ba[0] = 0xFF;
      Result of the output ba[0] is symbol "?".

      How did you output it? Your example works perfectly fine for me, I see 0xFF in the debugger.

      It means that it stored not 8 bit but 6, symbol "?" is 63 in ascii table and 00111111 in binary code.

      I rather think, your output cannot display the character for 0xFF and prints a '?' instead.

      Warning is C4309.

      I don't know what the text for this warning is, but I guess it's because QByteArray contains chars and char is signed for most compilers, so it has a range of -128...127. 0xFF is outside this range, hence the warning. You can help yourself with a cast: ba[0] = char(0xFF); or with a literal: ba[0] = '\xFF'

      How can i store all 8 bits in QByteArray ?

      You already do, it's just a matter of output.

      Qt has to stay free or it will die.

      Z 1 Reply Last reply
      4
      • aha_1980A aha_1980

        Hi @Zizione:

        QByteArray ba;
        ba.resize(4);
        ba[0] = 0xFF;
        Result of the output ba[0] is symbol "?".

        How did you output it? Your example works perfectly fine for me, I see 0xFF in the debugger.

        It means that it stored not 8 bit but 6, symbol "?" is 63 in ascii table and 00111111 in binary code.

        I rather think, your output cannot display the character for 0xFF and prints a '?' instead.

        Warning is C4309.

        I don't know what the text for this warning is, but I guess it's because QByteArray contains chars and char is signed for most compilers, so it has a range of -128...127. 0xFF is outside this range, hence the warning. You can help yourself with a cast: ba[0] = char(0xFF); or with a literal: ba[0] = '\xFF'

        How can i store all 8 bits in QByteArray ?

        You already do, it's just a matter of output.

        Z Offline
        Z Offline
        Zizione
        wrote on last edited by VRonin
        #3

        @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 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          welcome to the forum and you are jumping right into a controversial point of the API (see https://forum.qt.io/topic/89766/converting-qbytearray-to-unsigned-char/9). Basically QByteArray interface uses char while you are trying to use unsigned char and hence the warning.

          You can either:

          • ignore the warning (QByteArray is actually storing the correct value it is just casting it to signed)
          • use manual casting: *reinterpret_cast<unsigned char*>(ba.data()[0])=0xFF;
          • use memcpy: const unsigned char tempVal = 0xFF; std::memcpy(ba.data()+0,&tempVal ,1);

          "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

          Z 1 Reply Last reply
          5
          • VRoninV VRonin

            welcome to the forum and you are jumping right into a controversial point of the API (see https://forum.qt.io/topic/89766/converting-qbytearray-to-unsigned-char/9). Basically QByteArray interface uses char while you are trying to use unsigned char and hence the warning.

            You can either:

            • ignore the warning (QByteArray is actually storing the correct value it is just casting it to signed)
            • use manual casting: *reinterpret_cast<unsigned char*>(ba.data()[0])=0xFF;
            • use memcpy: const unsigned char tempVal = 0xFF; std::memcpy(ba.data()+0,&tempVal ,1);
            Z Offline
            Z Offline
            Zizione
            wrote on last edited by
            #5

            @VRonin So it is ok with storing 0xFF value , if i need this value in my code from QByteArray it would be given ?

            1 Reply Last reply
            0
            • 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 Online
                JonBJ Online
                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