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
QtWS25 Last Chance

QByteArray problem

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 4.8k 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 13 Apr 2018, 08:43 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)

    A 1 Reply Last reply 13 Apr 2018, 08:52
    0
    • Z Zizione
      13 Apr 2018, 08:43

      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)

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 13 Apr 2018, 08:52 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 13 Apr 2018, 08:55
      4
      • A aha_1980
        13 Apr 2018, 08:52

        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 13 Apr 2018, 08:55 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();
        }
        
        J 1 Reply Last reply 13 Apr 2018, 09:46
        0
        • V Offline
          V Offline
          VRonin
          wrote on 13 Apr 2018, 08:56 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 13 Apr 2018, 09:05
          5
          • V VRonin
            13 Apr 2018, 08:56

            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 13 Apr 2018, 09:05 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
            • V Offline
              V Offline
              VRonin
              wrote on 13 Apr 2018, 09:16 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
                13 Apr 2018, 08:55

                @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();
                }
                
                J Offline
                J Offline
                JonB
                wrote on 13 Apr 2018, 09:46 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 A 2 Replies Last reply 13 Apr 2018, 09:54
                1
                • J JonB
                  13 Apr 2018, 09:46

                  @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 Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 13 Apr 2018, 09:54 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
                  • J JonB
                    13 Apr 2018, 09:46

                    @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;
                    

                    ?

                    A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 13 Apr 2018, 10:12 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 1 Reply Last reply 13 Apr 2018, 10:38
                    0
                    • A aha_1980
                      13 Apr 2018, 10:12

                      @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 Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 13 Apr 2018, 10:38 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.

                      A 1 Reply Last reply 13 Apr 2018, 10:44
                      0
                      • J J.Hilk
                        13 Apr 2018, 10:38

                        @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

                        A Offline
                        A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on 13 Apr 2018, 10:44 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 1 Reply Last reply 13 Apr 2018, 10:51
                        1
                        • A aha_1980
                          13 Apr 2018, 10:44

                          @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 Offline
                          J Offline
                          J.Hilk
                          Moderators
                          wrote on 13 Apr 2018, 10:51 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.

                          A 1 Reply Last reply 13 Apr 2018, 11:01
                          0
                          • J J.Hilk
                            13 Apr 2018, 10:51

                            @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.

                            A Offline
                            A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on 13 Apr 2018, 11:01 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 1 Reply Last reply 13 Apr 2018, 11:13
                            0
                            • A aha_1980
                              13 Apr 2018, 11:01

                              @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 Offline
                              J Offline
                              J.Hilk
                              Moderators
                              wrote on 13 Apr 2018, 11:13 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.

                              A 1 Reply Last reply 13 Apr 2018, 11:20
                              0
                              • J J.Hilk
                                13 Apr 2018, 11:13

                                @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);

                                A Offline
                                A Offline
                                aha_1980
                                Lifetime Qt Champion
                                wrote on 13 Apr 2018, 11:20 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

                                6/15

                                13 Apr 2018, 09:16

                                • Login

                                • Login or register to search.
                                6 out of 15
                                • First post
                                  6/15
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved