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. Convert 64 Byte QByteArray to two 32 Byte int
Qt 6.11 is out! See what's new in the release blog

Convert 64 Byte QByteArray to two 32 Byte int

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 3.8k 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 Infinity
    #1

    I have a 64 byte long QByteArray. Something like this:

    QByteArray sig = QByteArray::fromHex("3045022100f209c63ebba931be2b6bb6fffbe1d7e45a45e9ea48b835280d954ca7058953eb02204a145da934c1104a4d2253663bc0db4136b52cd62fe32cf383");
    

    Now I have to split it into two 32 Byte long QByteArray's

    QByteArray r = sig.mid(0, 32);
    QByteArray s = sig.mid(32, 32);
    

    Then I have to convert the QByteArray r and s into a big int.

    And then I have to do some calculation on r and s and convert them back into a QByteArray.

    How can I do this?

    JonBJ 1 Reply Last reply
    0
    • I Infinity

      I have a 64 byte long QByteArray. Something like this:

      QByteArray sig = QByteArray::fromHex("3045022100f209c63ebba931be2b6bb6fffbe1d7e45a45e9ea48b835280d954ca7058953eb02204a145da934c1104a4d2253663bc0db4136b52cd62fe32cf383");
      

      Now I have to split it into two 32 Byte long QByteArray's

      QByteArray r = sig.mid(0, 32);
      QByteArray s = sig.mid(32, 32);
      

      Then I have to convert the QByteArray r and s into a big int.

      And then I have to do some calculation on r and s and convert them back into a QByteArray.

      How can I do this?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Infinity said in Convert 64 Byte QByteArray to two 32 Byte int:

      Now I have to split it into two 32 Byte long QByteArray's

      Then rather you intend: QByteArray s = sig.mid(32, 32);

      Then I have to convert the QByteArray r and s into a big int.

      Define "big int". You have 32 bytes (each) now. On a 64-bit machine ints go up to 8 bytes long. (Actually, it looks like mostly int will only be 32-bit, and even the largest language-supported long long [int] will, I think, only be 64-bits --- see e.g. https://en.cppreference.com/w/cpp/language/types.) So are you going to user some higher-precision numerical library? (E.g. I imagine boost might offer this.) " convert them back into a QByteArray" will depend on the package you use.

      I 1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        How should this fit into a int64_t? A 64-bit (signed) integer can be max. 9223372036854775807 (hex: 0x7FFFFFFFFFFFFFFF) and your QByteArray is 32 Bytes long.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • JonBJ JonB

          @Infinity said in Convert 64 Byte QByteArray to two 32 Byte int:

          Now I have to split it into two 32 Byte long QByteArray's

          Then rather you intend: QByteArray s = sig.mid(32, 32);

          Then I have to convert the QByteArray r and s into a big int.

          Define "big int". You have 32 bytes (each) now. On a 64-bit machine ints go up to 8 bytes long. (Actually, it looks like mostly int will only be 32-bit, and even the largest language-supported long long [int] will, I think, only be 64-bits --- see e.g. https://en.cppreference.com/w/cpp/language/types.) So are you going to user some higher-precision numerical library? (E.g. I imagine boost might offer this.) " convert them back into a QByteArray" will depend on the package you use.

          I Offline
          I Offline
          Infinity
          wrote on last edited by Infinity
          #4

          @JonB It works with boost cpp_int. How can I convert a cpp_int back to a QByteArray?

          aha_1980A 1 Reply Last reply
          0
          • I Infinity

            @JonB It works with boost cpp_int. How can I convert a cpp_int back to a QByteArray?

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Infinity If you can convert it to const char [] or std::string, then you can also create a QByteArray from it.

            Regards

            Qt has to stay free or it will die.

            I 1 Reply Last reply
            2
            • aha_1980A aha_1980

              @Infinity If you can convert it to const char [] or std::string, then you can also create a QByteArray from it.

              Regards

              I Offline
              I Offline
              Infinity
              wrote on last edited by
              #6

              @aha_1980 I found a way

              std::string str;
              std::stringstream stream;
              stream << std::hex << s;
              str = stream.str();
              
              QByteArray sByteArray = QByteArray::fromStdString(str);
              

              Is that correct or is there a cleaner way to do it?

              aha_1980A 1 Reply Last reply
              0
              • I Infinity

                @aha_1980 I found a way

                std::string str;
                std::stringstream stream;
                stream << std::hex << s;
                str = stream.str();
                
                QByteArray sByteArray = QByteArray::fromStdString(str);
                

                Is that correct or is there a cleaner way to do it?

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Infinity I'm not sure there is a cleaner way, but it looks like that could work.

                Regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Infinity
                  wrote on last edited by Infinity
                  #8

                  Here is how I solved the problem. Just in case someone else faces a similar problem.

                  QByteArray sig = QByteArray::fromHex("3045022100f209c63ebba931be2b6bb6fffbe1d7e45a45e9ea48b835280d954ca7058953eb02204a145da934c1104a4d2253663bc0db4136b52cd62fe32cf383");
                  
                  QByteArray rByteArray = sig.mid(0, 32);
                  QByteArray sByteArray = sig.mid(32, 32);
                  
                  qDebug().noquote() << "r hex      :" << rByteArray.toHex();
                  qDebug().noquote() << "s hex      :" << sByteArray.toHex();
                  
                  // Create a cpp_int
                  boost::multiprecision::cpp_int r("0x" + rByteArray.toHex().toStdString());
                  
                  // Create a cpp_int
                  boost::multiprecision::cpp_int s("0x" + sByteArray.toHex().toStdString());
                  
                  boost::multiprecision::cpp_int sum;
                  
                  sum = r + s;
                  
                  // Convert a cpp_int to a QByteArray
                  std::string str;
                  std::stringstream stream;
                  stream << std::hex << sum;
                  str = stream.str();
                  
                  return QByteArray::fromStdString(str);
                  
                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #9

                    Why to conversion to QString and std::string at all? cpp_int takes a const char * afaics.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    I 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      Why to conversion to QString and std::string at all? cpp_int takes a const char * afaics.

                      I Offline
                      I Offline
                      Infinity
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher I need to insert "0x" at the beginning. Could I also insert it directly in the QByteArray?

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Infinity said in Convert 64 Byte QByteArray to two 32 Byte int:

                        Could I also insert it directly in the QByteArray?

                        What's wrong with the documentation?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        I 1 Reply Last reply
                        1
                        • Christian EhrlicherC Christian Ehrlicher

                          @Infinity said in Convert 64 Byte QByteArray to two 32 Byte int:

                          Could I also insert it directly in the QByteArray?

                          What's wrong with the documentation?

                          I Offline
                          I Offline
                          Infinity
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher Thank you very much. I changed the example above accordingly. How does that look like?

                          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