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 a QString into a unsigned char array value
Forum Updated to NodeBB v4.3 + New Features

Convert a QString into a unsigned char array value

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.4k 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.
  • R Offline
    R Offline
    rtvideo
    wrote on last edited by
    #1

    Hello, I have three QString values I am grabbing from lineEdits:

    QString a = ui->lineEdit->text();
    QString b = ui->lineEdit_2->text();
    QString c= ui->lineEdit_3->text();
    

    I then convert them to strings:

    std::string a_str = a.toStdString();
    std::string b_str = b.toStdString();
    std::string c_str = c.toStdString();
    

    I create char values and pass the string into the char:

    char * a_char[20];
    char * b_char[20];
    char * c_char[20];
    
    strcpy((char *)a_char, a.c_str());
    strcpy((char *)b_char, b.c_str());
    strcpy((char *)c_char, c.c_str())
    

    I have a unsiged char array and want to pass in the values I got from the lineEdits into the array, passing in the chars as you can see below does not work, i get " error: cannot initialize an array element of type 'unsigned char' with an lvalue of type 'char *[20]'" how would I be able to adjust the code to do so?

    unsigned char data[3] = {a_char , b_char , c_char}
    
    SGaistS 1 Reply Last reply
    0
    • R rtvideo

      Hello, I have three QString values I am grabbing from lineEdits:

      QString a = ui->lineEdit->text();
      QString b = ui->lineEdit_2->text();
      QString c= ui->lineEdit_3->text();
      

      I then convert them to strings:

      std::string a_str = a.toStdString();
      std::string b_str = b.toStdString();
      std::string c_str = c.toStdString();
      

      I create char values and pass the string into the char:

      char * a_char[20];
      char * b_char[20];
      char * c_char[20];
      
      strcpy((char *)a_char, a.c_str());
      strcpy((char *)b_char, b.c_str());
      strcpy((char *)c_char, c.c_str())
      

      I have a unsiged char array and want to pass in the values I got from the lineEdits into the array, passing in the chars as you can see below does not work, i get " error: cannot initialize an array element of type 'unsigned char' with an lvalue of type 'char *[20]'" how would I be able to adjust the code to do so?

      unsigned char data[3] = {a_char , b_char , c_char}
      
      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      @rtvideo said in Convert a QString into a unsigned char array value:

      char * a_char[20];

      This an array of pointer to chars, not an array of chars.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R Offline
        R Offline
        rtvideo
        wrote on last edited by
        #3

        Do you know how I would be able to pass a value taken from the LineEdit and put it into the array?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          As silly as it may sound: use a char array.

          From the looks of it, you want to use arrays of 20 chars, fix the declaration.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rtvideo
            wrote on last edited by rtvideo
            #5

            I remove the pointer to make it char a[20]; When passing it in I get - error: cannot initialize an array element of type 'unsigned char' with an lvalue of type 'char [20]'

            Even trying to not use a unsigned char array I still get errors

            I added:

            const unsigned  char *a_i2c= reinterpret_cast<unsigned char*>(a_char);
            const unsigned  char *b_i2c= reinterpret_cast<unsigned char *>(b_char);
            const unsigned  char *c_i2c= reinterpret_cast<unsigned char *>(c_char);
            

            And it works now when passing in *a_i2c, *b_i2c, *c_i2c in the unsigned array, but for some reason all the values are coming out to be 48 when passing in different hex values

            JonBJ 1 Reply Last reply
            0
            • R rtvideo

              I remove the pointer to make it char a[20]; When passing it in I get - error: cannot initialize an array element of type 'unsigned char' with an lvalue of type 'char [20]'

              Even trying to not use a unsigned char array I still get errors

              I added:

              const unsigned  char *a_i2c= reinterpret_cast<unsigned char*>(a_char);
              const unsigned  char *b_i2c= reinterpret_cast<unsigned char *>(b_char);
              const unsigned  char *c_i2c= reinterpret_cast<unsigned char *>(c_char);
              

              And it works now when passing in *a_i2c, *b_i2c, *c_i2c in the unsigned array, but for some reason all the values are coming out to be 48 when passing in different hex values

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

              @rtvideo
              I hesitate to ask this, but why/do you need to do this conversion stuff to arrays of char? And then why do you want to go through the std::string layer?

              1 Reply Last reply
              1
              • R Offline
                R Offline
                rtvideo
                wrote on last edited by
                #7

                @JonB I need to pass an array of type unsigned char into a function. The way im doing the conversion might be doing a step I dont need your right, But I need to pull the data from the lineEdits that are typed in by the user (which will be hex values) and then pass them into the array so I can pass the array into a function for computation.

                JonBJ 1 Reply Last reply
                0
                • R rtvideo

                  @JonB I need to pass an array of type unsigned char into a function. The way im doing the conversion might be doing a step I dont need your right, But I need to pull the data from the lineEdits that are typed in by the user (which will be hex values) and then pass them into the array so I can pass the array into a function for computation.

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

                  @rtvideo
                  Well, you're also strcpy()ing them out, and into limited-size char arrays, and so on.

                  What about https://stackoverflow.com/questions/2523765/qstring-to-char-conversion

                  The easiest way to convert a QString to char* is qPrintable(const QString& str), which is a macro expanding to str.toLocal8Bit().constData().

                  Though be careful about the lifetime of the return result, as per the last comment there.

                  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