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 to quint16
Forum Updated to NodeBB v4.3 + New Features

QByteArray to quint16

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 717 Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on last edited by
    #1

    Hi,

    I wrote code like below to change QByteArray to quint16 variable:

    QByteArray data;
       data.resize(1);
       data[0] = 0x08;
    
       QDataStream stream(&data, QIODevice::ReadOnly);
       stream.setByteOrder(QDataStream::BigEndian);
    
       quint16 var;
    
       stream >> var;
    
       qDebug() << "Expected var = 8, but var = " << var;
    

    But output is:

    Expected var = 8, but var =  0
    

    I know that quint16 is written on two bytes and QByteArray has one byte, but this is part of a larger code and I may happen that QByteArray will have two bytes as below:

    QByteArray data;
        data.resize(2);
        data[0] = 0x08;
        data[1] = 0x04;
    

    Then the output is correctly and var = 2052. How could I write a QByteArray with one byte to quint16?

    jsulmJ JonBJ JKSHJ 3 Replies Last reply
    0
    • C Creatorczyk

      Hi,

      I wrote code like below to change QByteArray to quint16 variable:

      QByteArray data;
         data.resize(1);
         data[0] = 0x08;
      
         QDataStream stream(&data, QIODevice::ReadOnly);
         stream.setByteOrder(QDataStream::BigEndian);
      
         quint16 var;
      
         stream >> var;
      
         qDebug() << "Expected var = 8, but var = " << var;
      

      But output is:

      Expected var = 8, but var =  0
      

      I know that quint16 is written on two bytes and QByteArray has one byte, but this is part of a larger code and I may happen that QByteArray will have two bytes as below:

      QByteArray data;
          data.resize(2);
          data[0] = 0x08;
          data[1] = 0x04;
      

      Then the output is correctly and var = 2052. How could I write a QByteArray with one byte to quint16?

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

      @Creatorczyk Does this work:

      quint8 var;
      stream >> var;
      quint16 var16 = var;
      

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

      C 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Creatorczyk Does this work:

        quint8 var;
        stream >> var;
        quint16 var16 = var;
        
        C Offline
        C Offline
        Creatorczyk
        wrote on last edited by
        #3

        @jsulm Yes, it's works, but can I do this without creating quint8?

        jsulmJ 1 Reply Last reply
        0
        • C Creatorczyk

          @jsulm Yes, it's works, but can I do this without creating quint8?

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

          @Creatorczyk said in QByteArray to quint16:

          but can I do this without creating quint8?

          I guess you need two bytes in the QByteArray to make it work (fill it with a zero byte).
          I don't know what QDataStream does if you ask it to give you a short but the underlying QByteArray contains only one byte (you can check the QDataStream source code if you want to know what happens).

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

          1 Reply Last reply
          3
          • C Creatorczyk

            Hi,

            I wrote code like below to change QByteArray to quint16 variable:

            QByteArray data;
               data.resize(1);
               data[0] = 0x08;
            
               QDataStream stream(&data, QIODevice::ReadOnly);
               stream.setByteOrder(QDataStream::BigEndian);
            
               quint16 var;
            
               stream >> var;
            
               qDebug() << "Expected var = 8, but var = " << var;
            

            But output is:

            Expected var = 8, but var =  0
            

            I know that quint16 is written on two bytes and QByteArray has one byte, but this is part of a larger code and I may happen that QByteArray will have two bytes as below:

            QByteArray data;
                data.resize(2);
                data[0] = 0x08;
                data[1] = 0x04;
            

            Then the output is correctly and var = 2052. How could I write a QByteArray with one byte to quint16?

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

            @Creatorczyk said in QByteArray to quint16:

            How could I write a QByteArray with one byte to quint16?

            quint16 var16 = qByteArray[0]?

            As @jsulm has said, I don't think you're supposed to stream one byte into a two-byte variable. Sounds like it does something like only go into the first byte at the address, or maybe even does nothing at all, at least something dodgy.

            1 Reply Last reply
            2
            • C Creatorczyk

              Hi,

              I wrote code like below to change QByteArray to quint16 variable:

              QByteArray data;
                 data.resize(1);
                 data[0] = 0x08;
              
                 QDataStream stream(&data, QIODevice::ReadOnly);
                 stream.setByteOrder(QDataStream::BigEndian);
              
                 quint16 var;
              
                 stream >> var;
              
                 qDebug() << "Expected var = 8, but var = " << var;
              

              But output is:

              Expected var = 8, but var =  0
              

              I know that quint16 is written on two bytes and QByteArray has one byte, but this is part of a larger code and I may happen that QByteArray will have two bytes as below:

              QByteArray data;
                  data.resize(2);
                  data[0] = 0x08;
                  data[1] = 0x04;
              

              Then the output is correctly and var = 2052. How could I write a QByteArray with one byte to quint16?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @Creatorczyk said in QByteArray to quint16:

              How could I write a QByteArray with one byte to quint16?

              You cannot do that directly with a QDataStream. There are not enough bytes, so you will get a QDataStream::ReadPastEnd error: https://doc.qt.io/qt-5/qdatastream.html#Status-enum

              @jsulm suggested that you study the source code; the code for QDataStream::operator>>() is at https://code.woboq.org/qt5/qtbase/src/corelib/serialization/qdatastream.cpp.html#_ZN11QDataStreamrsERs

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              JonBJ VRoninV 2 Replies Last reply
              4
              • JKSHJ JKSH

                @Creatorczyk said in QByteArray to quint16:

                How could I write a QByteArray with one byte to quint16?

                You cannot do that directly with a QDataStream. There are not enough bytes, so you will get a QDataStream::ReadPastEnd error: https://doc.qt.io/qt-5/qdatastream.html#Status-enum

                @jsulm suggested that you study the source code; the code for QDataStream::operator>>() is at https://code.woboq.org/qt5/qtbase/src/corelib/serialization/qdatastream.cpp.html#_ZN11QDataStreamrsERs

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

                @JKSH
                Ah, I wondered whether it set an error and why the OP received 0 as the result here. Answer from the code you referenced:

                    if (readBlock(reinterpret_cast<char *>(&i), 2) != 2) {
                        i = 0;
                

                That's why the result is 0, and readBlock() sets error flag from trying to read 2 bytes but only getting 1 via

                    const int readResult = dev->read(data, len);
                    if (readResult != len)
                        setStatus(ReadPastEnd);
                
                1 Reply Last reply
                2
                • JKSHJ JKSH

                  @Creatorczyk said in QByteArray to quint16:

                  How could I write a QByteArray with one byte to quint16?

                  You cannot do that directly with a QDataStream. There are not enough bytes, so you will get a QDataStream::ReadPastEnd error: https://doc.qt.io/qt-5/qdatastream.html#Status-enum

                  @jsulm suggested that you study the source code; the code for QDataStream::operator>>() is at https://code.woboq.org/qt5/qtbase/src/corelib/serialization/qdatastream.cpp.html#_ZN11QDataStreamrsERs

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @JKSH said in QByteArray to quint16:

                  There are not enough bytes, so you will get a QDataStream::ReadPastEnd

                  For Qt 5.7 or later:

                  quint16 var;
                  stream.startTransaction();
                  stream >> var;
                  if(!stream.commitTransaction()){
                      quint8 tempVar;
                      stream.startTransaction();
                      stream >> tempVar;
                      if(!stream.commitTransaction()){
                          //handle this error
                      }
                      var=tempVar;
                  }
                  

                  "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

                  • Login

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