QChar zu wchar_t
Frage
Ich brauche eine QChar zu einem wchar_t
konvertierenIch habe versucht, die folgenden:
#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, und bevor schlägt jemand die .toWCharArray mit (wchar_t * array) Funktion, habe ich versucht, und es funktioniert im Wesentlichen dasselbe wie oben und überträgt keine Zeichen, wie es sollte.
Unten ist der Code für dass, wenn Sie mir nicht glauben:
#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;
}
Bitte Hilfe, ich habe seit einigen Tagen in diesem einfachen Problem. Ich würde im Idealfall wie nicht verwenden Wchar_t überhaupt ist aber leider ein Zeiger auf diese Art in einer dritten Partei-Funktion erforderlich, um eine Pumpe mit seriellen RS232-Befehlen zu steuern.
Danke.
EDIT: Um diesen Code laufen Sie die QT-Bibliotheken benötigen, können Sie diese durch das Herunterladen von Qt Creator bekommen kann und die Ausgabe in der Konsole erhalten Sie hinzufügen müssen, um den Befehl „CONFIG + = Konsole“ auf die .pro Datei (in QT Konzept) oder auf die benutzerdefinierten Definitionen unter Eigenschaften, wenn ein netBeans-Projekt verwendet wird.
EDIT:
Dank Vlad unten für seine korrekte Antwort:
Hier ist der aktualisierte Code, um das Gleiche zu tun, sondern eine Übertragung char von char-Methode und die Erinnerung an die Null-Terminierung hinzuzufügen.
#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;
}
Lösung
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.