QChar到wchar_t [英] QChar to wchar_t

查看:830
本文介绍了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\\\
;
wchar_t myArray [mystring.size()];

for(int x = 0; x< mystring.size(); x ++)
{
myArray [x] = mystring.at(x).toLatin1();
cout<< mystring.at(x).toLatin1(); //检查索引x处的字符(罚)
}

cout<< myArray:< myArray<< \\\
; //不给我正确的值
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\\\
;
cout<< mystring.toLatin1()。data();
wchar_t mywcharArray [mystring.size()];
cout<< Mystring size:< mystring.size()<< \\\
;
int length = -1;
length = mystring.toWCharArray(mywcharArray);
cout<< length:<长度;
cout<< mywcharArray;

return 0;

}

请帮助,我一直在这个简单的问题天。我最好不想使用wchar_t,但不幸的是,在第三方函数中需要使用串口RS232命令来控制泵。



谢谢。 。



编辑:要运行这个代码,你将需要QT库,你可以通过下载QT creator获得这些,并得到控制台中的输出你将有如果使用netbeans项目,请将命令CONFIG + = console添加到.pro文件(在QT创建者中)或属性下的自定义定义。



/ p>

感谢Vlad正确的回应:



这里是更新的代码做同样的事情, a char 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\\\
;
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'; //将空字符添加到wchar数组的结尾
wcout<< myArray:< myArray<< \\\
; // use wcout to output wchar_t's

return 0;
}


解决方案

QString 添加到 std :: wstring wchar_t 数组:

  #include< iostream> 
#include< QtCore / QString>

using namespace std;

int main()
{
//创建QT字符串。
QString qstr =Hello World;

//将其转换为标准wstring。
std :: wstring str = qstr.toStdWString();

//获取指向标准字符串的wchar_t指针。
const wchar_t * p = str.c_str();

//输出结果...
wcout<< str<< endl;
wcout<< p < endl;

//转换为Wchar_t的C数组的示例。
wchar_t array [qstr.size()+ 1];
int length = qstr.toWCharArray(array);
if(length< 0 || length> = sizeof(array)/ sizeof(array [0]))
{
cerr< 转换失败...< endl;
return 1;
}

array [length] ='\0'; //手动放置终止字符。
wcout<< array<< endl; // Output ...
}

请注意,将 QString 到数组更容易出错,要输出unicode字符串,你必须使用std::wcout 而不是 std :: cout ,这是相同的输出流,但 wchar_t 。 / p>

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

解决方案

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.

这篇关于QChar到wchar_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆