2010-09-16 61 views
6

我需要一个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; 
} 
+0

像Vlad指出的那样,除了使用cout作为wchar_t字符串输出外,你的代码基本上是正确的。将最后一个cout改为wcout会给你正确的结果。 – 2010-09-16 13:49:06

回答

7

这里是转换QString到两个std::wstringwchar_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

+0

这个'wchar_t数组[qstr.size()+ 1];'不是C,它不是C++。 – 2014-04-17 04:49:35

+0

@ BenVoigt:当然,这是VLA。很快就会用C++! – 2014-04-17 13:03:42

+1

编号C++将具有类似的功能,但'sizeof'(您的答案使用)不适用于C++运行时大小的自动数组。当然,你对sizeof的使用是毫无意义的,就好像这个条件被违反了,你已经超过了缓冲区,并且你无法做很多事情来恢复。 OTOH ['toWCharArray'文档](http://qt-project.org/doc/qt-4.8/qstring.html#toWCharArray)对所需的最大长度做出承诺,因此可以简单地删除条件。 – 2014-04-17 13:20:14