需要将 char* (指针)转换为 wchar_t* (指针)
我正在与计算机控制的泵进行一些串行端口通信,并且用于通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。
我还使用 QT 创建一个表单并获取 QString 形式的 com 端口名称。
该 QString 转换为字符数组并指向如下:
char* Dialog::GetPumpSerialPortNumber(){
QString mystring;
mystring = ui->comboBox_2->currentText();
char * mychar;
mychar = mystring.toLatin1().data();
return mychar;
我现在需要设置端口号,该端口号作为 wchar_t* 存储在我的泵对象中。我通过调用以下函数来做到这一点:
void pump::setPortNumber(wchar_t* portNumber){
this->portNumber = portNumber;
}
那么如何将我的 char* (mychar) 更改为 wchar_t* (portNumber)?
谢谢。
I am doing some serial port communcation to a computer controlled pump and the createfile function I used to communicate requires the com port name to be parsed as a wchar_t pointer.
I am also using QT to create a form and aquire the com port name as a QString.
This QString is converted to a char array and pointed to as follows:
char* Dialog::GetPumpSerialPortNumber(){
QString mystring;
mystring = ui->comboBox_2->currentText();
char * mychar;
mychar = mystring.toLatin1().data();
return mychar;
I now need to set my port number which is stored as a wchar_t* in my pump object. I do this by calling the following function:
void pump::setPortNumber(wchar_t* portNumber){
this->portNumber = portNumber;
}
Thus how do I change my char* (mychar) into a wchar_t* (portNumber)?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您只是需要将 char 数组转换为 wchar_t 数组,那么这里有一个解决方案:
If you're talking about just needing a char array to a wchar_t array, here's a solution for you:
对leetNightshade答案的增强可能是
其中unistrlen将返回字符串中有多少个字符(单字节或多字节字符),这与strlen不同,strlen按字节返回长度,如果字符串包含一些多字节字符,则可能会浪费一些内存。
An enhancement to leetNightshade's answer could be
Where unistrlen will return how many characters (single or multi bytes characters) in your string unlike strlen which returns the length byte by byte and that might waste some memory if your string contains some multi-byte characters.
您可以使用
toWCharArray
函数QString
获取 wchar_t* 值并从GetPumpSerialPortNumber
函数返回 wchar_t*。You can use the
toWCharArray
function ofQString
to have your wchar_t* value and return a wchar_t* from yourGetPumpSerialPortNumber
function.我在 MSDN 中找到了一篇有用的文章 - 如何:在各种字符串类型之间进行转换。我想应该是有用的。
I've found a helpful article in MSDN - How to: Convert Between Various String Types. I guess it should be useful.
QString::toWCharArray ( wchar_t * array )
?QString::toWCharArray ( wchar_t * array )
?