我需要一个QChar则转换为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的*阵列)功能,我我已经试过了,它基本上和上面做的一样,不会像它应该那样传输字符。
下面是该代码,如果你不相信我:
#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创建程序获取这些代码,并在控制台中获取输出,您将不得不向命令添加命令“CONFIG + = console”。 pro文件(在QT创建者中)或者如果使用NetBeans项目,则属性下的自定义定义。
编辑:
感谢弗拉德以下为他的正确反应:
这里是更新的代码做同样的事情,但使用传输字符由字符的方法和记忆加空终止。
#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;
}
像Vlad指出的那样,除了使用cout作为wchar_t字符串输出外,你的代码基本上是正确的。将最后一个cout改为wcout会给你正确的结果。 – 2010-09-16 13:49:06