我有以下代码:
inline bool match(const std::wstring & text1, const std::wstring & text2)
{
return match(text1.c_str(), text2.c_str());
}
inline bool match(const std::wstring & text1, const wchar_t * text2)
{
return match(text1.c_str(), text2);
}
inline bool match(const wchar_t * text1, const std::wstring & text2)
{
return match(text1, text2.c_str());
}
inline bool match(const wchar_t * text1, const wchar_t * text2)
{
return !wcscmp(text1, text2);
}
Run Code Online (Sandbox Code Playgroud)
我得到:
error C2666: 'match' : 3 overloads have similar conversions
1> could be 'bool match(const wchar_t *,const std::wstring &)' …Run Code Online (Sandbox Code Playgroud) 我使用日语字符串作为wchar_t,我需要将其转换为char*.是否有任何方法或功能转换wchar_t*为char*不丢失数据?
很多时候,当我与人交谈时,他们正在重新使用(在DLL项目中)wstrings和字符串,因为它们比wchar_t数组消耗更多内存.怎么样?L"qweqweqweqweqwe"wchar_t数组是否需要比wstring更多的内存,或者它是不明显的?当我必须在Ansi和Unicode(很多new wchar_t temp和delete)之间进行转换时,我有点困惑
我目前正在研究一个C ++项目,在该项目中我需要显示一些扩展字符(wchar_t)。
主要的问题是,即使它在C(使用wprintf)中可以正常工作,也不能在使用mvwaddwstr或的c ++中运行waddwstr。当然,我已将语言环境设置为: setlocale(LC_ALL, "");,并且不显示任何内容。
有人以前有这个问题吗,或者对此有想法?
谢谢。
这是代码:
struct charMap { int x; int y; wchar_t value };
int i, x, y;
wchar_t str[2];
struct charMap _charMap[2] = {
{0,0,9474}
{29, 29, 9474}
};
initscr();
setlocale(LC_ALL, "");
for (y = 0 ; y < 30 /* length */ + 2 ; y++) {
for (x = 0 ; x < 30 /* width */ + 2; x++) {
for (i = 0 …Run Code Online (Sandbox Code Playgroud) 所以我想在注册表中添加一个字符串,因为注册表字符串要写入NULL终止我的字符串在各个地方包含一个空字符.
这是我的字符串的样子.
char names[550] = "1DFA-3327-*\01DFA-3527-*\001DFA-E527-*\00951-1500-
我把它转换成whcar_t字符串就像这样.
wchar_t names_w[1000];
size_t charsConverted = 0;
mbstowcs_s(&charsConverted, names_w, names, SIZE);
RegSetValueEx(*pKeyHandle, valueName, 0, REG_MULTI_SZ, (LPBYTE)names_w, size);
Run Code Online (Sandbox Code Playgroud)
注册表项应该是
1DFA-3327-*
1DFA-3527-*
1DFA-E527-*
0951-1500-*
0951-0004-*
0951-160D-*
Run Code Online (Sandbox Code Playgroud)
但这是注册表项,
1DFA-3327-*
<a box here>DFA-3527-*
<a box here>DFA-E527-*
951-1500-*
951-0004-*
951-160D-*
Run Code Online (Sandbox Code Playgroud)
所以吃起来0的0951也吃了1在1DFA
我尝试过的:
1>我尝试将字符串更改为
char names[550] = "1DFA-3327-*\0\01DFA-3527-*\0\001DFA-E527-*\0\00951-1500-
^ ^ Two nulls
Run Code Online (Sandbox Code Playgroud)
2>我尝试了不同的转换.
for(int i; i < SIZE; i++)
names_w[i] = (wchar_t)names[i];
Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用模板化运算符<< function的日志类.我专门研究宽字符串的模板函数,这样我就可以在写日志消息之前做一些从宽到窄的翻译.我不能让TCHAR正常工作 - 它不使用专业化.想法?
这是相关的代码:
// Log.h header
class Log
{
public:
template <typename T> Log& operator<<( const T& x );
template <typename T> Log& operator<<( const T* x );
template <typename T> Log& operator<<( const T*& x );
...
}
template <typename T> Log& Log::operator<<( const T& input )
{ printf("ref"); }
template <typename T> Log& Log::operator<<( const T* input )
{ printf("ptr"); }
template <> Log& Log::operator<<( const std::wstring& input );
template <> Log& Log::operator<<( const wchar_t* input …Run Code Online (Sandbox Code Playgroud) 我用mingw编译器.如何转换wchar_t为char?
Data(HWND hwnd, wchar_t szFileName[MAX_PATH])
{
string sIn;
ifstream infile;
infile.open(szFileName);
infile.seekg(0,ios::beg);
// fill vector with file rows
while ( getline(infile,sIn ) )
{
fileRows.push_back(sIn);
}
}
Run Code Online (Sandbox Code Playgroud)
我想将wchar_t szFileName转换为char szFileNameChar.
在我的代码中,我有一个wchar_t数组:
wchar_t paths [6] = {L"C:\\Program Files\\SomeAppsSuiteFolder1", L"C:\\Program Files\\SomeAppsSuiteFolder2", L"C:\\Program Files (x86)\\SomeAppsSuiteFolder1", L"C:\\Program Files (x86)\\SomeAppsSuiteFolder2", L"C:\\SomeAppsSuiteFolder1", L"C:\\SomeAppsSuiteFolder2"};
Run Code Online (Sandbox Code Playgroud)
后来我在for循环中使用数组.问题是,对于这一行,我得到以下错误:
error: too many initializers for 'wchar_t [6]'
error: initializer-string for array of chars is too long [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
更重要的是,在for循环中我有这样的if条件:
if(GetFileAttributesW(paths[i])!=INVALID_FILE_ATTRIBUTES) {...}
Run Code Online (Sandbox Code Playgroud)
而且,我在这里得到一个错误:
error: invalid conversion from 'wchar_t' to 'LPCWSTR {aka const wchar_t*}' [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
奇怪的是,类似的代码几个月前用于正确编译......有什么问题?
我正在从我下载的一些代码中重写一个C++方法.该方法最初将PCWSTR作为参数,然后提示用户输入文件名.我修改了方法以获取两个参数(均为PCWSTR)而不是提示用户.我已经在其他地方生成了文件列表.我试图用我的迭代文件列表的方法中的两个参数调用我的新(修改)方法.
原始方法使用StringCBGetsW命令提示用户输入.像这样...
HRESULT tst=S_OK; //these are at the top of the method
WCHAR fname[85] = {0}; //these are at the top of the method
tst = StringCbGetsW(fname,sizeof(fname));
Run Code Online (Sandbox Code Playgroud)
wchar fname被传递给另一个迭代方法.当我看到那个方法时,它说它是LPCWSTR类型; 我假设它可以取代WCHAR.
但它不能做的就是采用该方法获得的PCWSTR.我的最终目标是尝试不提示用户输入文件名,而是取代之前在另一种方法中迭代的文件名.
TL;博士.我有一个PCWSTR,它需要转换为WCHAR.我不知道WCHAR []是什么或如何用它做什么.包括尝试做printf以查看它是什么.
PS ...我知道有更简单的方法来移动和复制文件,有一个原因我试图使用程序来完成这项工作.
众所周知,要打印变量值,类型是固定宽度整数类型之一(如uint32_t),您需要包含cinttypes(在C++中)或inttypes.h(在C中)头文件,并使用格式说明符宏PRIu32.但是在使用wprintf函数时如何做同样的事情?L在这种情况下,这样的宏应该扩展为带有前缀的字符串文字.