在过去的一周里,我和朋友一起用C++ 编写了一个roguelike游戏.大部分也学习语言.
我正在使用:
wchar_t在控制台中输出我想要的任何地方.我成功输出了一些unicode字符,如\ u263B(☻),但其他如\ u2638(☸)最终会以问号(?)结束.
这是我用于输出的相关代码.
// Container of room information
struct RoomInfo
{
wchar_t * layout;
int width;
int height;
};
// The following function builds RoomInfo
RoomInfo Room::examine(IActor * examinor)
{
RoomInfo ri;
ri.width = this->width;
ri.height = this->height;
ri.layout = new wchar_t[height * width];
for(unsigned int y = 0; y < height; y++)
{
for(unsigned int x = 0; x < width; x++)
{
ri.layout[y*width + x] …Run Code Online (Sandbox Code Playgroud) 我需要将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;
}
Run Code Online (Sandbox Code Playgroud)
哦,在有人建议使用.toWCharArray(wchar_t*array)函数之前,我已经尝试过了,它基本上做了与上面相同的事情,并没有按照它应该传输字符.
如果你不相信我,下面是代码:
#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>
using namespace std;
int main(int argc, char** argv) { …Run Code Online (Sandbox Code Playgroud) 你好我有一个泵类需要使用一个成员变量,该变量是一个指向包含端口地址的wchar_t数组的指针,即:"com9".
问题是,当我在构造函数中初始化此变量时,我的编译器会标记折旧的转换警告.
pump::pump(){
this->portNumber = L"com9";}
Run Code Online (Sandbox Code Playgroud)
这工作正常但每次编译时的警告都很烦人,让我觉得我做错了什么.
我尝试创建一个数组,然后像这样设置成员变量:
pump::pump(){
wchar_t port[] = L"com9";
this->portNumber = port;}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,这使我的portNumber指向'F'.
显然,我的另一个概念问题.
感谢您对我的无聊问题的帮助.
编辑:
作为请求,portNumber的定义是:
class pump
{
private:
wchar_t* portNumber;
}
Run Code Online (Sandbox Code Playgroud)
感谢答案,它现在已更改为:
class pump
{
private:
const wchar_t* portNumber;
}
Run Code Online (Sandbox Code Playgroud) 如果我想将一段字符串转换为UTF-16,比如说char * xmlbuffer,我是否必须wchar_t *在编码之前将类型转换为UTF-16?char*在编码为UTF-8之前是否需要输入类型?
怎么样wchar_t,char与UTF-8或UTF-16或UTF-32或其他转换格式有关?
在此先感谢您的帮助!
在这个环节 unsigned wchar_t被typedef编为WCHAR.但我无法在我的SDK winnt.h或mingw中找到这种typedef winnt.h.
wchar_t 是签名还是未签名?
我在C语言中使用WINAPI.
我被要求为现有的旧项目添加功能,但我无法构建它.它处理unicode字符串但我在使用TCHAR时遇到很多错误.具体而言,几乎每个错误都是TCHAR无法转换为或用作wchar_t.从我在许多不同的文章中看到我尝试使用#define _UNICODE或#define UNICODE,但没有一个解决了这个问题.
这是一段代码:
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <strsafe.h>
#include <string>
#include <list>
#include <cctype>
#include <winnt.h>
#include <atlconv.h>
#pragma comment(lib,"wininet.lib")
using namespace std;
TCHAR *tags[] = { _T("aa"), _T("bb"), _T("cc"),
NULL };
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
for (i = 1; i<argc; i++) {
if (wcscmp(argv[i], _T("-h")) == 0) {
...
}
else if (wcscmp(argv[i], _T("-f")) == 0) {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
在上面的行中,例如,当使用wcscmp时,我得到了
argument of type …Run Code Online (Sandbox Code Playgroud) 我想要一个希腊字母数组,这就是我所做的:
wchar_t pcletters[30] = {'?' , '?' , '?' , '?' , '?' , '?' , '?' , '?', '?' , '?' , '?' , '?' , '?' , '?','?' , '?' , '?' , '?' , '?' , '?' , '?' , '?' , '?' , '?' , '?', '?' , '?' , '?' , '?' , '?' , '?'} ;
Run Code Online (Sandbox Code Playgroud)
我也包括 <locale.h>并且有一条线setlocale(LC_CTYPE, "") .
但是我得到警告警告:多字符字符常量[-Wmultichar].此外,当我通过执行以下操作检查其中一个字母是否在用户输入中时:
if (userword[i] == pcletters[j]) {//do stuff}
Run Code Online (Sandbox Code Playgroud)
它似乎不起作用. - 为什么我会收到这个警告? - 有没有办法改变pcletters的方式,以便能够比较 …
我需要一个C++代码来将给定的字符串转换wchar_t*为UTF-16字符串.它必须适用于Windows和Linux.我在搜索过程中浏览了很多网页,但这个主题对我来说仍然不清楚.
据我所知,我需要:
setlocale使用LC_TYPE和UTF-16编码进行呼叫.wcstombs转换wchar_t为UTF-16字符串.setlocale以恢复以前的区域设置.你知道我可以wchar_t*以便携方式(Windows和Linux)转换为UTF-16的方式吗?
我正在尝试编写一些只有用户可以移植的C代码gcc,并且已glib安装.
从我的所有研究中,我发现gcca,a wchar_t总是定义为4个字节,gliba gunichar也是4个字节.
我没想到的是,如果像a gunichar,a wchar_t也被编码为UCS4.是这样的吗?如果是这样,我应该能够简单地转换gunichar*为a wchar_t*并使用这些stdc wcs*函数,对吗?
我想将QString变量传递给带const wchar_t*参数的函数.
安全解决方案是:
void foo(const wchar_t*);
QString x = "test";
foo(x.toStdWString().c_str());
Run Code Online (Sandbox Code Playgroud)
但它有转换成的开销wstring.有没有更快的解决方案?
这个解决方案怎么样?它安全便携吗?
foo(reinterpret_cast<const wchar_t*>(x.constData()));
Run Code Online (Sandbox Code Playgroud)