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. Bytes array to QByteArray.

Bytes array to QByteArray.

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 5.1k 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.
  • JonBJ JonB

    @jenya7
    Yes, you do "miss something"!

    Both uint8_t and char are a byte with 8 bits in it. The unsigned/signed are a matter of how something might interpret the byte/char, but they remain the same 8-bit pattern. No "data is lost" when you cast between them. Just trust me on this! :)

    When you cast you would be better using reinterpret_cast<>() than static_cast <>().

    J Offline
    J Offline
    jenya7
    wrote on last edited by jenya7
    #5

    @JonB
    This way

    Data.append( QByteArray( static_cast<char*>(encrypted), MIN_CRYPT_SIZE ) );
    

    I get - static_cast from 'uint8_t *' (aka 'unsigned char *') to 'char *' is not allowed

    J.HilkJ 1 Reply Last reply
    0
    • JonBJ JonB

      @jenya7
      Yes, you do "miss something"!

      Both uint8_t and char are a byte with 8 bits in it. The unsigned/signed are a matter of how something might interpret the byte/char, but they remain the same 8-bit pattern. No "data is lost" when you cast between them. Just trust me on this! :)

      When you cast you would be better using reinterpret_cast<>() than static_cast <>().

      J Offline
      J Offline
      jenya7
      wrote on last edited by
      #6

      @JonB said in Bytes array to QByteArray.:

      When you cast you would be better using reinterpret_cast<>() than static_cast <>().

      I see. Thank you. It works.

      JonBJ 1 Reply Last reply
      0
      • J jenya7

        @JonB
        This way

        Data.append( QByteArray( static_cast<char*>(encrypted), MIN_CRYPT_SIZE ) );
        

        I get - static_cast from 'uint8_t *' (aka 'unsigned char *') to 'char *' is not allowed

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

        @jenya7 you would need reinterpret cast and an additional const_cast.

        You shouldn't be doing that anyway, use memcpy. More verbose to write, but more compiler robust, and probably just as fast.


        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.

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

          @jenya7 you would need reinterpret cast and an additional const_cast.

          You shouldn't be doing that anyway, use memcpy. More verbose to write, but more compiler robust, and probably just as fast.

          J Offline
          J Offline
          jenya7
          wrote on last edited by
          #8

          @J-Hilk
          Like this?

          memcpy(Data.data(), encrypted, MIN_CRYPT_SIZE);
          

          I like it better.

          JonBJ KroMignonK 2 Replies Last reply
          0
          • J jenya7

            @JonB said in Bytes array to QByteArray.:

            When you cast you would be better using reinterpret_cast<>() than static_cast <>().

            I see. Thank you. It works.

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

            @jenya7

            Data.append( reinterpret_cast<char*>(encrypted), MIN_CRYPT_SIZE );
            

            using QByteArray &QByteArray::append(const char *str, int len) is least typing.

            Or since you don't use the QByteArray Data; before this, and so don't need append(), you could just move the declaration to here:

            QByteArray Data( reinterpret_cast<char*>(encrypted), MIN_CRYPT_SIZE );
            
            1 Reply Last reply
            0
            • J jenya7

              @J-Hilk
              Like this?

              memcpy(Data.data(), encrypted, MIN_CRYPT_SIZE);
              

              I like it better.

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

              @jenya7 said in Bytes array to QByteArray.:

              memcpy(Data.data(), encrypted, MIN_CRYPT_SIZE);
              I like it better.

              What? Please don't go into using memcpy() when no need, this code does not append anything (you were appending), or does not set the size (will overflow unallocated memory) in the QByteArray (unless pre-sized).

              1 Reply Last reply
              0
              • J jenya7

                @J-Hilk
                Like this?

                memcpy(Data.data(), encrypted, MIN_CRYPT_SIZE);
                

                I like it better.

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #11

                @jenya7 said in Bytes array to QByteArray.:

                 memcpy(Data.data(), encrypted, MIN_CRYPT_SIZE);
                

                I like it better.

                This is very dangerous!
                You have to ensure that buffer holden by Data is big enough to write the data (Data.size() >= MIN_CRYPT_SIZE) or you will corrupt memory!

                EDIT
                My suggestion for your code is:

                void UDP_Send(QByteArray data, QString remote_ip, quint16 remote_port)
                {
                    QByteArray encrypted(CRYPT_SIZE, 0);
                    
                    CRYPTO_Encrypt(reinterpret_cast<uint8_t *>(encrypted.data()), 
                                   encrypted.size(), 
                                   reinterpret_cast<uint8_t *>(data.data()),
                                   data.size());
                    
                    udp_socket->writeDatagram(encrypted, QHostAddress(remote_ip), remote_port);
                }
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                J 1 Reply Last reply
                1
                • KroMignonK KroMignon

                  @jenya7 said in Bytes array to QByteArray.:

                   memcpy(Data.data(), encrypted, MIN_CRYPT_SIZE);
                  

                  I like it better.

                  This is very dangerous!
                  You have to ensure that buffer holden by Data is big enough to write the data (Data.size() >= MIN_CRYPT_SIZE) or you will corrupt memory!

                  EDIT
                  My suggestion for your code is:

                  void UDP_Send(QByteArray data, QString remote_ip, quint16 remote_port)
                  {
                      QByteArray encrypted(CRYPT_SIZE, 0);
                      
                      CRYPTO_Encrypt(reinterpret_cast<uint8_t *>(encrypted.data()), 
                                     encrypted.size(), 
                                     reinterpret_cast<uint8_t *>(data.data()),
                                     data.size());
                      
                      udp_socket->writeDatagram(encrypted, QHostAddress(remote_ip), remote_port);
                  }
                  
                  J Offline
                  J Offline
                  jenya7
                  wrote on last edited by
                  #12

                  @KroMignon
                  I see. Thank you.
                  I thought about

                  Data.resize(CRYPT_SIZE);
                  

                  But this way it's less coding.

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @jenya7
                    Yes, you do "miss something"!

                    Both uint8_t and char are a byte with 8 bits in it. The unsigned/signed are a matter of how something might interpret the byte/char, but they remain the same 8-bit pattern. No "data is lost" when you cast between them. Just trust me on this! :)

                    When you cast you would be better using reinterpret_cast<>() than static_cast <>().

                    J Offline
                    J Offline
                    jenya7
                    wrote on last edited by
                    #13

                    @JonB said in Bytes array to QByteArray.:

                    @jenya7
                    Yes, you do "miss something"!

                    Both uint8_t and char are a byte with 8 bits in it. The unsigned/signed are a matter of how something might interpret the byte/char, but they remain the same 8-bit pattern. No "data is lost" when you cast between them. Just trust me on this! :)

                    When you cast you would be better using reinterpret_cast<>() than static_cast <>().

                    Following the logic.
                    Slave sends (not Qt, embedded code)

                    float fval = SensorGetValue();
                    
                    ival = (int)fval;
                                
                    udp_tx_buffer[5] = ival;
                    udp_tx_buffer[6] = ival >> 8;
                    udp_tx_buffer[7] = ival >> 16;
                    udp_tx_buffer[8] = ival >> 24;  
                    

                    On Qt side I get

                    float fval  = static_cast<float>( (sens_msg->data[3] << 24) | (sens_msg->data[2] << 16) | (sens_msg->data[1]<< 8) | sens_msg->data[0]);
                    

                    But I get it truncated to integer.

                    KroMignonK JonBJ 2 Replies Last reply
                    0
                    • J jenya7

                      @JonB said in Bytes array to QByteArray.:

                      @jenya7
                      Yes, you do "miss something"!

                      Both uint8_t and char are a byte with 8 bits in it. The unsigned/signed are a matter of how something might interpret the byte/char, but they remain the same 8-bit pattern. No "data is lost" when you cast between them. Just trust me on this! :)

                      When you cast you would be better using reinterpret_cast<>() than static_cast <>().

                      Following the logic.
                      Slave sends (not Qt, embedded code)

                      float fval = SensorGetValue();
                      
                      ival = (int)fval;
                                  
                      udp_tx_buffer[5] = ival;
                      udp_tx_buffer[6] = ival >> 8;
                      udp_tx_buffer[7] = ival >> 16;
                      udp_tx_buffer[8] = ival >> 24;  
                      

                      On Qt side I get

                      float fval  = static_cast<float>( (sens_msg->data[3] << 24) | (sens_msg->data[2] << 16) | (sens_msg->data[1]<< 8) | sens_msg->data[0]);
                      

                      But I get it truncated to integer.

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by KroMignon
                      #14

                      @jenya7 said in Bytes array to QByteArray.:

                      On Qt side I get
                      float fval = static_cast<float>( (sens_msg->data[3] << 24) | (sens_msg->data[2] << 16) | (sens_msg->data[1]<< 8) | sens_msg->data[0]);

                      But I get it truncated to integer.

                      This totally wrong, sens_msg->data[] is a 8 bit value, so shifting it to left with 8 bit (or more) will fill it with '0'.
                      You could cast each byte to an integer (before shifting) or use QDataStream:

                      QDataStream stream(&sens_msg, QIODevice::ReadOnly);
                      
                      float fval;
                      stream >> fval;
                      

                      cf. documentation https://doc.qt.io/qt-5/qdatastream.html.

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply
                      1
                      • J jenya7

                        @JonB said in Bytes array to QByteArray.:

                        @jenya7
                        Yes, you do "miss something"!

                        Both uint8_t and char are a byte with 8 bits in it. The unsigned/signed are a matter of how something might interpret the byte/char, but they remain the same 8-bit pattern. No "data is lost" when you cast between them. Just trust me on this! :)

                        When you cast you would be better using reinterpret_cast<>() than static_cast <>().

                        Following the logic.
                        Slave sends (not Qt, embedded code)

                        float fval = SensorGetValue();
                        
                        ival = (int)fval;
                                    
                        udp_tx_buffer[5] = ival;
                        udp_tx_buffer[6] = ival >> 8;
                        udp_tx_buffer[7] = ival >> 16;
                        udp_tx_buffer[8] = ival >> 24;  
                        

                        On Qt side I get

                        float fval  = static_cast<float>( (sens_msg->data[3] << 24) | (sens_msg->data[2] << 16) | (sens_msg->data[1]<< 8) | sens_msg->data[0]);
                        

                        But I get it truncated to integer.

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

                        @jenya7
                        If your sender & receiver are on same architecture, so you don't care about any possible byte-ordering issues (I don't know whether they apply to float-types anyway), you could just send 4 bytes from &udp_tx_buffer[5] and receive as 4 bytes into &fval. No individual bytes.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jenya7
                          wrote on last edited by
                          #16

                          OK, I'll try it.

                          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