views:

154

answers:

1
+1  Q: 

QChar to wchar_t

Hello,

I need to convert a QChar to a wchar_t

I've tried the following:

#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {

    QString mystring = "Hello World\n";
    wchar_t myArray[mystring.size()];

    for (int x=0; x<mystring.size(); x++)
    {
        myArray[x] = mystring.at(x).toLatin1();
        cout << mystring.at(x).toLatin1(); // checks the char at index x (fine)
    }

    cout << "myArray : " << myArray << "\n"; // doesn't give me correct value
    return 0;
}

Oh and before someone suggests using the .toWCharArray(wchar_t* array) function, I've tried that and it essentially does the same thing as above and does not transfer characters as it should.

Below is the code for that if you don't believe me:

#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {
QString mystring = "Hello World\n";
cout << mystring.toLatin1().data();
wchar_t mywcharArray[mystring.size()];
cout << "Mystring size : " << mystring.size() << "\n";
int length = -1;
length = mystring.toWCharArray(mywcharArray);
cout << "length : " << length;    
cout << mywcharArray;

return 0;

}

Please help, I've been at this simple problem for days. I'd ideally like to not use wchar_t's at all but unfortunately a pointer to this type is required in a third party function to control a pump using serial RS232 commands.

Thanks.

EDIT: To run this code you will need the QT libraries, you can get these by downloading QT creator and to get the output in the console you'll have to add the command "CONFIG += console" to the .pro file (in QT creator) or to the custom definitions under properties if using a netbeans project.

EDIT:

Thanks to Vlad below for his correct response:

Here is the updated code to do the same thing but using a transfer char by char method and remembering to add the null termination.

#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {


    QString mystring = "Hello World\n";
    wchar_t myArray[mystring.size()];

    for (int x=0; x<mystring.size(); x++)
    {
        myArray[x] = (wchar_t)mystring.at(x).toLatin1();
        cout << mystring.at(x).toLatin1();
    }

    myArray[mystring.size()-1] = '\0';  // Add null character to end of wchar array
    wcout << "myArray : " << myArray << "\n"; // use wcout to output wchar_t's

    return 0;
}
+2  A: 

Here is an example of converting QString into both std::wstring and wchar_t array:

#include <iostream>
#include <QtCore/QString>

using namespace std;

int main ()
{
    // Create QT string.
    QString qstr = "Hello World";

    // Convert it into standard wstring.
    std::wstring str = qstr.toStdWString ();

    // Get the wchar_t pointer to the standard string.
    const wchar_t *p = str.c_str ();

    // Output results...
    wcout << str << endl;
    wcout << p << endl;

    // Example of converting to C array of wchar_t.
    wchar_t array[qstr.size () + 1];
    int length = qstr.toWCharArray (array);
    if (length < 0 || length >= sizeof(array) / sizeof (array[0]))
    {
        cerr << "Conversion failed..." << endl;
        return 1;
    }

    array[length] = '\0'; // Manually put terminating character.
    wcout << array << endl; // Output...
}

Please note that converting QString into array is more error-prone and that to output unicode string, you have to use std::wcout instead of std::cout, which is the same output stream but for wchar_t.

Vlad Lazarenko