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. How to generate a QByteArray from std::string which contains \0x00?
Forum Updated to NodeBB v4.3 + New Features

How to generate a QByteArray from std::string which contains \0x00?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 615 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by
    #1

    The function m_cryptoNewKeyResponse->public_key() returns a std::string which actually contains '\0x00' characters.

    qDebug() << "public key length:" <<  m_cryptoNewKeyResponse->public_key().length();
    // Oputpu: public key length: 88
    QByteArray publicKey(m_cryptoNewKeyResponse->public_key().data());
    qDebug() << "public key length:" <<  publicKey.length();
    // Output: public key length: 18
    qDebug() << "public key raw:" <<  publicKey;
    // Output: public key raw: 3056301006072a8648ce3d020106052b8104
    

    How can I generate a QByteArray which also contains the '\0x00'? As you can see above, the QBytearray cuts the off everything which is after the first '\0x00'.

    J.HilkJ 1 Reply Last reply
    0
    • I Infinity

      This the output of the qDebug()

      public key length: 88
      public key length: 18
      public key raw hex: "30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04"

      That is the output with

      qDebug() << "public key raw:" <<  publicKey.toHex(' ');
      // Output: public key raw hex: "30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04"
      

      Why does

      m_cryptoNewKeyResponse->public_key().length();
      //Output: public key length: 18
      

      return 88 (which is correct)

      and:

      QByteArray publicKey(m_cryptoNewKeyResponse->public_key().data());
      qDebug() << "public key length:" <<  publicKey.length();
      //Output: public key length: 18
      

      return 18?

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

      @Infinity std::string::data() return as const char * so QByteArray(const char *) will stops a first '\0' found.
      To do it right use auto publicKey = QByteArray(m_cryptoNewKeyResponse->public_key().data(), m_cryptoNewKeyResponse->public_key().length())

      See QByteArray() documentation

      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
      6
      • I Infinity

        The function m_cryptoNewKeyResponse->public_key() returns a std::string which actually contains '\0x00' characters.

        qDebug() << "public key length:" <<  m_cryptoNewKeyResponse->public_key().length();
        // Oputpu: public key length: 88
        QByteArray publicKey(m_cryptoNewKeyResponse->public_key().data());
        qDebug() << "public key length:" <<  publicKey.length();
        // Output: public key length: 18
        qDebug() << "public key raw:" <<  publicKey;
        // Output: public key raw: 3056301006072a8648ce3d020106052b8104
        

        How can I generate a QByteArray which also contains the '\0x00'? As you can see above, the QBytearray cuts the off everything which is after the first '\0x00'.

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

        @Infinity said in How to generate a QByteArray from std::string which contains \0x00?:

        How can I generate a QByteArray which also contains the '\0x00'? As you can see above, the QBytearray cuts the off everything which is after the first '\0x00'.

        no it does not, qDebug however does.

        try

        qDebug() << "public key raw:" <<  publicKey.toHex(' ');
        

        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
        4
        • I Offline
          I Offline
          Infinity
          wrote on last edited by Infinity
          #3

          This the output of the qDebug()

          public key length: 88
          public key length: 18
          public key raw hex: "30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04"

          That is the output with

          qDebug() << "public key raw:" <<  publicKey.toHex(' ');
          // Output: public key raw hex: "30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04"
          

          Why does

          m_cryptoNewKeyResponse->public_key().length();
          //Output: public key length: 18
          

          return 88 (which is correct)

          and:

          QByteArray publicKey(m_cryptoNewKeyResponse->public_key().data());
          qDebug() << "public key length:" <<  publicKey.length();
          //Output: public key length: 18
          

          return 18?

          J.HilkJ KroMignonK 2 Replies Last reply
          0
          • I Infinity

            This the output of the qDebug()

            public key length: 88
            public key length: 18
            public key raw hex: "30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04"

            That is the output with

            qDebug() << "public key raw:" <<  publicKey.toHex(' ');
            // Output: public key raw hex: "30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04"
            

            Why does

            m_cryptoNewKeyResponse->public_key().length();
            //Output: public key length: 18
            

            return 88 (which is correct)

            and:

            QByteArray publicKey(m_cryptoNewKeyResponse->public_key().data());
            qDebug() << "public key length:" <<  publicKey.length();
            //Output: public key length: 18
            

            return 18?

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

            @Infinity probably an implicit conversation error.

            You said, that m_cryptoNewKeyResponse->public_key() returns an std::string?

            than try the following:

            QByteArray publicKey = QByteArray::fromStdString(m_cryptoNewKeyResponse->public_key());
            

            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
            0
            • I Infinity

              This the output of the qDebug()

              public key length: 88
              public key length: 18
              public key raw hex: "30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04"

              That is the output with

              qDebug() << "public key raw:" <<  publicKey.toHex(' ');
              // Output: public key raw hex: "30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04"
              

              Why does

              m_cryptoNewKeyResponse->public_key().length();
              //Output: public key length: 18
              

              return 88 (which is correct)

              and:

              QByteArray publicKey(m_cryptoNewKeyResponse->public_key().data());
              qDebug() << "public key length:" <<  publicKey.length();
              //Output: public key length: 18
              

              return 18?

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

              @Infinity std::string::data() return as const char * so QByteArray(const char *) will stops a first '\0' found.
              To do it right use auto publicKey = QByteArray(m_cryptoNewKeyResponse->public_key().data(), m_cryptoNewKeyResponse->public_key().length())

              See QByteArray() documentation

              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
              6

              • Login

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