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 convert a quint32 to QByteArray without leading 00?
Forum Updated to NodeBB v4.3 + New Features

How to convert a quint32 to QByteArray without leading 00?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 800 Views 2 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

    I have a quint32 with the value of 43. Now I would like to convert it to a QByteArray. I tried it like this:

    quint32 newChainIdInt = 43;
    
    QByteArray newChainIdArray;
    QDataStream stream(&newChainIdArray, QIODevice::WriteOnly);
    stream << newChainIdInt;
    
    qDebug() << newChainIdArray.toHex(' ');
    

    I get the following output:

    00 00 00 2b
    

    I need to have it without leading 00 00 00. It should look like this:

    2b
    

    How can I do this?

    KroMignonK 1 Reply Last reply
    0
    • I Infinity

      I have a quint32 with the value of 43. Now I would like to convert it to a QByteArray. I tried it like this:

      quint32 newChainIdInt = 43;
      
      QByteArray newChainIdArray;
      QDataStream stream(&newChainIdArray, QIODevice::WriteOnly);
      stream << newChainIdInt;
      
      qDebug() << newChainIdArray.toHex(' ');
      

      I get the following output:

      00 00 00 2b
      

      I need to have it without leading 00 00 00. It should look like this:

      2b
      

      How can I do this?

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

      @Infinity said in How to convert a quint32 to QByteArray without leading 00?:

      How can I do this?

      Cast the value to an char:

      uint32 newChainIdInt = 43;
      
      QByteArray newChainIdArray;
      QDataStream stream(&newChainIdArray, QIODevice::WriteOnly);
      stream << quint8(newChainIdInt);
      
      

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

      I 1 Reply Last reply
      0
      • KroMignonK KroMignon

        @Infinity said in How to convert a quint32 to QByteArray without leading 00?:

        How can I do this?

        Cast the value to an char:

        uint32 newChainIdInt = 43;
        
        QByteArray newChainIdArray;
        QDataStream stream(&newChainIdArray, QIODevice::WriteOnly);
        stream << quint8(newChainIdInt);
        
        
        I Offline
        I Offline
        Infinity
        wrote on last edited by
        #3

        @KroMignon Thanks. What if the value would be greater than a quint8?

        KroMignonK 1 Reply Last reply
        0
        • I Infinity

          @KroMignon Thanks. What if the value would be greater than a quint8?

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

          @Infinity said in How to convert a quint32 to QByteArray without leading 00?:

          What if the value would be greater than a quint8?

          It will be lost, of course!
          You can also remove zero from QByteArray:

          uint32 newChainIdInt = 43;
          
          QByteArray newChainIdArray;
          QDataStream stream(&newChainIdArray, QIODevice::WriteOnly);
          stream << newChainIdInt;
          while(newChainIdArray.size() > 1 && newChainIdArray[0] == 0)
              newChainIdArray.remove(0);
          qDebug() << newChainIdArray.toHex(' ');
          

          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
          0
          • M Offline
            M Offline
            mcleary
            wrote on last edited by mcleary
            #5

            I use the following piece of code

            double value = 43;
            QByteArray byteArray{ reinterpret_cast<char*>(&value), sizeof(value) };
            

            This should work for anything that is contiguous in memory.

            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