Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Convert a QString into a unsigned char array value



  • 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}
    

  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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.



  • 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



  • @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?



  • @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.



  • @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.


Log in to reply