QChar a wchar_t
Domanda
Ho bisogno di convertire un QChar a un wchar_t
Ho provato quanto segue:
#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, e prima che qualcuno suggerisce di utilizzare il .toWCharArray (wchar_t * array) la funzione, ho provato questo e lo fa essenzialmente la stessa cosa come sopra e non trasferisce i caratteri come dovrebbe.
Di seguito è riportato il codice per che se non mi credete:
#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;
}
Si prega di aiuto, sono stato a questo semplice problema per giorni. Mi piacerebbe idealmente come non utilizzare wchar_t è a tutti, ma purtroppo un puntatore a questo tipo è richiesto in una funzione di terze parti per il controllo di una pompa utilizzando i comandi seriali RS232.
Grazie.
EDIT: Per eseguire questo codice si ha bisogno delle librerie QT, è possibile ottenere questi scaricando Qt Creator e per ottenere l'output nella console dovrete aggiungere il comando "CONFIG + = console" al .pro file (in Qt Creator) o alle definizioni personalizzati in proprietà se si utilizza un progetto NetBeans.
EDIT:
Grazie a Vlad di sotto per la sua risposta corretta:
Ecco il codice aggiornato per fare la stessa cosa, ma con un carattere di trasferimento con il metodo char e ricordando di aggiungere la terminazione null.
#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;
}
Soluzione
Ecco un esempio di conversione di QString in entrambi std :: wstring e wchar_t matrice:
#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...
}
nota che la conversione QString nella matrice è più soggetto a errori e che all'uscita stringa unicode, è necessario utilizzare std :: wcout anziché std :: cout , che è lo stesso flusso di uscita, ma per wchar_t .