QChar to wchar_t
質問
QCHARをWCHAR_Tに変換する必要があります
以下を試しました:
#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;
}
ああ、誰かが.towChararray(wchar_t* array)関数を使用することを提案する前に、私はそれを試してみましたが、基本的に上記と同じことをして、必要な文字を転送しません。
あなたが私を信じていないなら、以下はそのコードです:
#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;
}
助けてください、私はこの単純な問題に何日も行ってきました。私は理想的にはWCHAR_Tをまったく使用したくないのですが、残念ながら、このタイプへのポインターは、シリアルRS232コマンドを使用してポンプを制御するためにサードパーティの関数で必要です。
ありがとう。
編集:このコードを実行するには、QTライブラリが必要になり、QTクリエーターをダウンロードし、コンソールで出力を取得することでこれらを取得できます。 QT Creator)またはNetBeansプロジェクトを使用する場合のプロパティの下のカスタム定義に。
編集:
彼の正しい応答について以下のVladに感謝します:
同じことを行うための更新されたコードは次のとおりですが、charメソッドによる転送charを使用し、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;
}
解決
これが変換の例です QSTRING 両方に std :: wstring と wchar_t 配列:
#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...
}
変換に注意してください QSTRING 配列にはよりエラーが発生しやすく、Unicode文字列を出力するには、使用する必要があります std :: wcout それ以外の std :: cout, 、これは同じ出力ストリームですが、 wchar_t.
所属していません StackOverflow