QChar 到 wchar_t

发布于 2024-09-19 21:27:35 字数 2158 浏览 1 评论 0原文

我需要将 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 创建者来获取这些库,并在控制台中获取输出,您必须将命令“CONFIG += console”添加到 .pro 文件中(在QT 创建者)或属性下的自定义定义(如果使用 netbeans 项目)。

编辑:

感谢下面的 Vlad 的正确响应:

这是执行相同操作的更新代码,但使用逐个字符传输方法并记住添加空终止。

#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;
}

I need to convert a QChar to a wchar_t

I've tried the following:

#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 and before someone suggests using the .toWCharArray(wchar_t* array) function, I've tried that and it essentially does the same thing as above and does not transfer characters as it should.

Below is the code for that if you don't believe me:

#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;

}

Please help, I've been at this simple problem for days. I'd ideally like to not use wchar_t's at all but unfortunately a pointer to this type is required in a third party function to control a pump using serial RS232 commands.

Thanks.

EDIT: To run this code you will need the QT libraries, you can get these by downloading QT creator and to get the output in the console you'll have to add the command "CONFIG += console" to the .pro file (in QT creator) or to the custom definitions under properties if using a netbeans project.

EDIT:

Thanks to Vlad below for his correct response:

Here is the updated code to do the same thing but using a transfer char by char method and remembering to add the null termination.

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-09-26 21:27:41

以下是将 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< /a> 进入数组更容易出错,要输出 unicode 字符串,您必须使用 std::wcout 而不是 std:: cout,它是相同的输出流,但适用于 wchar_t

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文