Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qstring to unsigned char conversion function
Forum Update on Monday, May 27th 2025

Qstring to unsigned char conversion function

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
14 Posts 4 Posters 5.7k 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.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by JKSH
    #1

    Dear all,

    i have this variable:

    unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
    

    and using this function to convert it to QString:

    QString CharToString(unsigned char *str)
    {
        QString result = "";
        int lengthOfString = strlen(reinterpret_cast<const char*>(str));
    
        // print string in reverse order
        QString s;
        for(int i = 0; i < lengthOfString; i++)
        {
            s = QString("%1").arg(str[i], 0, 16);
    
            // account for single-digit hex values (always must serialize as two digits)
            if(s.length() == 1)
                result.append("0");
    
            result.append(s);
        }
    
        return result;
    }
    

    now I am trying to figure out SAME function to convert the QString again to unsigned char.

    Im very lost and tried many solutions i found online, nothing worked...

    Can you pleas ehelp me to make new funciton StringToChar() to convert the Qstring into unsigned char?

    Thank you!!

    aha_1980A 1 Reply Last reply
    0
    • S shokarta

      Dear all,

      i have this variable:

      unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
      

      and using this function to convert it to QString:

      QString CharToString(unsigned char *str)
      {
          QString result = "";
          int lengthOfString = strlen(reinterpret_cast<const char*>(str));
      
          // print string in reverse order
          QString s;
          for(int i = 0; i < lengthOfString; i++)
          {
              s = QString("%1").arg(str[i], 0, 16);
      
              // account for single-digit hex values (always must serialize as two digits)
              if(s.length() == 1)
                  result.append("0");
      
              result.append(s);
          }
      
          return result;
      }
      

      now I am trying to figure out SAME function to convert the QString again to unsigned char.

      Im very lost and tried many solutions i found online, nothing worked...

      Can you pleas ehelp me to make new funciton StringToChar() to convert the Qstring into unsigned char?

      Thank you!!

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

      Hi @shokarta,

      if I get it right, all you want to do is to convert the array plain[] to a hexadecimal string.

      You can easily do this with: QString s = QByteArray(reinterpret_cast<const char*>(plain)).toHex() instead your complicated function.

      The other way round can be done with QByteArray::fromHex():

      const QString s ="0123456789ABCDEF";
      const QByteArray ba = QByteArray::fromHex(s.toLatin1());
      const unsigned char *plain = reinterpret_cast<const unsigned char *>(ba.constData());
      

      Regards

      Qt has to stay free or it will die.

      S 1 Reply Last reply
      1
      • aha_1980A aha_1980

        Hi @shokarta,

        if I get it right, all you want to do is to convert the array plain[] to a hexadecimal string.

        You can easily do this with: QString s = QByteArray(reinterpret_cast<const char*>(plain)).toHex() instead your complicated function.

        The other way round can be done with QByteArray::fromHex():

        const QString s ="0123456789ABCDEF";
        const QByteArray ba = QByteArray::fromHex(s.toLatin1());
        const unsigned char *plain = reinterpret_cast<const unsigned char *>(ba.constData());
        

        Regards

        S Offline
        S Offline
        shokarta
        wrote on last edited by
        #3

        @aha_1980
        Hello,

        many thanks for helping, so I have this then:

        unsigned char StringToChar(QString *str)
        {
            const QByteArray ba = QByteArray::fromHex(str->toLatin1());
            const unsigned char result[] = reinterpret_cast<const unsigned char*>(ba.constData());
        
            return result;
        }
        

        which brings error on line const unsigned char:
        error: array initializer must be an initializer list or string literal

        aha_1980A 1 Reply Last reply
        0
        • S shokarta

          @aha_1980
          Hello,

          many thanks for helping, so I have this then:

          unsigned char StringToChar(QString *str)
          {
              const QByteArray ba = QByteArray::fromHex(str->toLatin1());
              const unsigned char result[] = reinterpret_cast<const unsigned char*>(ba.constData());
          
              return result;
          }
          

          which brings error on line const unsigned char:
          error: array initializer must be an initializer list or string literal

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

          @shokarta

          Sorry, I missed a star, I meant const unsigned char *plain = reinterpret_cast<const unsigned char *>(ba.constData());

          Fixed my example.

          unsigned char StringToChar(QString *str)

          Why do you pass str by pointer here?

          const unsigned char result = reinterpret_cast<const unsigned char>(ba.constData());
          return result;

          Note that you cannot do that! the local variable is destroyed when you leave the function. You can only copy the result into a buffer provided from outside.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            shokarta
            wrote on last edited by shokarta
            #5

            @aha_1980

            the pointer was placed because QT gives me error and suggest to do that...

            so basicaly when u do this:

            unsigned char StringToChar(QString *str)
            {
                const QByteArray ba = QByteArray::fromHex(str.toLatin1());
                const unsigned char *result = reinterpret_cast<const unsigned char *>(ba.constData());
                return result;
            }
            

            i have error on line const QbyteArray ba:

            main.cpp:23:50: error: member reference type 'QString *' is a pointer; did you mean to use '->'?
            

            and another error on line return result:

            main.cpp:25:12: error: cannot initialize return object of type 'unsigned char' with an lvalue of type 'const unsigned char *'
            
            aha_1980A 1 Reply Last reply
            0
            • S shokarta

              @aha_1980

              the pointer was placed because QT gives me error and suggest to do that...

              so basicaly when u do this:

              unsigned char StringToChar(QString *str)
              {
                  const QByteArray ba = QByteArray::fromHex(str.toLatin1());
                  const unsigned char *result = reinterpret_cast<const unsigned char *>(ba.constData());
                  return result;
              }
              

              i have error on line const QbyteArray ba:

              main.cpp:23:50: error: member reference type 'QString *' is a pointer; did you mean to use '->'?
              

              and another error on line return result:

              main.cpp:25:12: error: cannot initialize return object of type 'unsigned char' with an lvalue of type 'const unsigned char *'
              
              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @shokarta

              the pointer was placed because QT gives me error and suggest to do that...

              That is no reason to do it like this. Fix it please. Either use QString str or (better) const QString &str as parameter.

              main.cpp:25:12: error: cannot initialize return object of type 'unsigned char' with an lvalue of type 'const unsigned char *'

              That is because the return value does not match the functions signature.

              But again: you cannot return a pointer to a local variable! You can use it within the function, but not outside.

              Better explain first why you need const unsigned char * at all, then we can find a solution.

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              1
              • S Offline
                S Offline
                shokarta
                wrote on last edited by
                #7

                basicaly i hae dowloaded and used huge function to AES encode and decode string.
                the function is loaded by unsigned char:

                        const unsigned int BLOCK_BYTES_LENGTH = 16 * sizeof(unsigned char);
                        unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
                        unsigned char iv[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
                        unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
                          0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
                        unsigned int len;
                
                        AES aes(256);
                        unsigned char *out = aes.EncryptCBC(plain, BLOCK_BYTES_LENGTH, key, iv, len);
                

                as well as it outputs the encrypted string in unsigned char

                Hoewer because its a huge function which i understand just on basic logical concept i prefer not to use modify the function to insert string instead of char, as well as output, so i would prefer to convert it (i will be using this on many places, therefore the function is desired)

                so then i could only use:

                unsigned char *out = aes.EncryptCBC(StringToChat(plain), BLOCK_BYTES_LENGTH, StringToChat(key), StringToChat(iv), len);
                
                aha_1980A 1 Reply Last reply
                0
                • S shokarta

                  basicaly i hae dowloaded and used huge function to AES encode and decode string.
                  the function is loaded by unsigned char:

                          const unsigned int BLOCK_BYTES_LENGTH = 16 * sizeof(unsigned char);
                          unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
                          unsigned char iv[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
                          unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
                            0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
                          unsigned int len;
                  
                          AES aes(256);
                          unsigned char *out = aes.EncryptCBC(plain, BLOCK_BYTES_LENGTH, key, iv, len);
                  

                  as well as it outputs the encrypted string in unsigned char

                  Hoewer because its a huge function which i understand just on basic logical concept i prefer not to use modify the function to insert string instead of char, as well as output, so i would prefer to convert it (i will be using this on many places, therefore the function is desired)

                  so then i could only use:

                  unsigned char *out = aes.EncryptCBC(StringToChat(plain), BLOCK_BYTES_LENGTH, StringToChat(key), StringToChat(iv), len);
                  
                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @shokarta said in Qstring to unsigned char conversion function:

                  aes.EncryptCBC

                  can you please show the function signature of that function?

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    shokarta
                    wrote on last edited by
                    #9

                    its downloaded and used from here:
                    https://github.com/SergeyBel/AES

                    i have done no futher changes on these files

                    aha_1980A 1 Reply Last reply
                    0
                    • S shokarta

                      its downloaded and used from here:
                      https://github.com/SergeyBel/AES

                      i have done no futher changes on these files

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

                      @shokarta

                      So the signature is:

                      unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)

                      None of these parameters is const, so that means the function can modify in, key, and iv (not sure if it does modify these parameters, but it could).

                      That means, you have to create local buffers for these parameters, and copy the data into the buffers.

                      Especially, this

                      unsigned char *out = aes.EncryptCBC(StringToChat(plain), BLOCK_BYTES_LENGTH, StringToChat(key), StringToChat(iv), len);

                      is not possible!

                      Regards

                      Qt has to stay free or it will die.

                      S 1 Reply Last reply
                      1
                      • aha_1980A aha_1980

                        @shokarta

                        So the signature is:

                        unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)

                        None of these parameters is const, so that means the function can modify in, key, and iv (not sure if it does modify these parameters, but it could).

                        That means, you have to create local buffers for these parameters, and copy the data into the buffers.

                        Especially, this

                        unsigned char *out = aes.EncryptCBC(StringToChat(plain), BLOCK_BYTES_LENGTH, StringToChat(key), StringToChat(iv), len);

                        is not possible!

                        Regards

                        S Offline
                        S Offline
                        shokarta
                        wrote on last edited by
                        #11

                        @aha_1980
                        Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
                        For skilled one it could be matrer of several minutes only :/.
                        I know its not nice to ask for such a modification, but this would be extreme helpfull :(.
                        Any chance you and i can have any deal on that?
                        Only thus one aes-cbc-256 function would be needed, other methods not at all...

                        aha_1980A jsulmJ JonBJ 3 Replies Last reply
                        0
                        • S shokarta

                          @aha_1980
                          Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
                          For skilled one it could be matrer of several minutes only :/.
                          I know its not nice to ask for such a modification, but this would be extreme helpfull :(.
                          Any chance you and i can have any deal on that?
                          Only thus one aes-cbc-256 function would be needed, other methods not at all...

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

                          @shokarta

                          If you want to use security functions, you should stick with well known and good tested libraries like openssl.

                          Using a not widely used lib or modifying it yourself is just a hazard game.

                          That said, I'm not even sure if Qt SLL would provide something useful for you.

                          Regards

                          Qt has to stay free or it will die.

                          1 Reply Last reply
                          0
                          • S shokarta

                            @aha_1980
                            Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
                            For skilled one it could be matrer of several minutes only :/.
                            I know its not nice to ask for such a modification, but this would be extreme helpfull :(.
                            Any chance you and i can have any deal on that?
                            Only thus one aes-cbc-256 function would be needed, other methods not at all...

                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @shokarta said in Qstring to unsigned char conversion function:

                            Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?

                            Why? This library does not use Qt. Simply use these functions the way they are supposed to be used.
                            Also, https://github.com/SergeyBel/AES/blob/master/README.md shows how to use the functions.

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

                            1 Reply Last reply
                            0
                            • S shokarta

                              @aha_1980
                              Would it be very dificult to modify those aes.* in order to put inside the signature not unsigned char but qstring directly?
                              For skilled one it could be matrer of several minutes only :/.
                              I know its not nice to ask for such a modification, but this would be extreme helpfull :(.
                              Any chance you and i can have any deal on that?
                              Only thus one aes-cbc-256 function would be needed, other methods not at all...

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

                              @shokarta
                              If you want a changed signature in a library, especially if it's just one function, the usual way is you write the desired wrapper function and go via that. Is there any reason you cannot do this for your usage? Over the years I have found it's often a good idea to write wrapper functions/classes for more or less every external library one uses, for all sorts of reasons. Heck, I don't even call the Qt classes/methods directly, I tend to provide wrappers for all of them!

                              1 Reply Last reply
                              3

                              • Login

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