Windows 如何将WCHAR*转换为常规字符串?

Windows 如何将WCHAR*转换为常规字符串?,windows,winapi,casting,wchar,Windows,Winapi,Casting,Wchar,因此,在Win32 API中,我的主函数定义如下: wmain(int argc,WCHAR*argv[] 我正在向它传递一些参数,我想根据参数的值执行一个switch case,类似这样 wmain(int argc, WCHAR* argv[]) { char* temp = argv[]; switch (temp) { case "one": blah blah; ... } 当然,temp=argv[]不起作用,我正在寻找转换它的建议。现在我有一个if-els

因此,在Win32 API中,我的主函数定义如下:

wmain(int argc,WCHAR*argv[]

我正在向它传递一些参数,我想根据参数的值执行一个switch case,类似这样

wmain(int argc, WCHAR* argv[])
{
    char* temp = argv[];
    switch (temp) {
    case "one": blah blah;
...
}
当然,temp=argv[]不起作用,我正在寻找转换它的建议。现在我有一个if-else-if的事情正在进行,这是非常低效的

我需要转换它的原因是因为我无法在WCHAR*上执行开关案例


谢谢您的关注。

我不确定这是否是一个好主意。WCHAR*可能包含无法以有意义的方式映射到char*的unicode字符。如果你想忽略这一点,在论坛上有一篇帖子,其中有一些关于从WCHAR*转换为char*

的建议,你也不能在char*上执行切换。(但当您实际需要将WCHAR*转换为char*时,请使用WideCharToMultiByte)

您需要使用if/else if with或其他字符串比较函数


或者,使用诸如或之类的参数解析器库,尝试将其从std::wstring转换为std::string,这很简单,也许有一种更短的方法


使用std::wstring constructor将WCHAR*转换为std::wstring,然后使用std::wstring方法之一转换为std::String

创建新的win32 console应用程序并选择ATL支持。添加此项并编译/运行

#include "stdafx.h"
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
// A _TCHAR is a typedef'd, depending on whether you've got a unicode or MBCS build

// ATL Conversion macros are documented here
// http://msdn.microsoft.com/en-us/library/87zae4a3(VS.80).aspx
// Declare USES_CONVERSION in your function before using the ATL conversion macros
// e.g. T2A(), A2T()    
USES_CONVERSION;

TCHAR* pwHelloWorld = _T("hello world!");
wcout << pwHelloWorld << endl;

// convert to char
char* pcHelloWorld = T2A(pwHelloWorld);
cout << pcHelloWorld << endl;


cin.get();

return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
//A_TCHAR是typedef'd,这取决于您是使用unicode还是MBCS构建
//这里记录了ATL转换宏
// http://msdn.microsoft.com/en-us/library/87zae4a3(第80节)
//在使用ATL转换宏之前,Declare在函数中使用_转换
//例如T2A(),A2T()
使用_转换;
TCHAR*pwHelloWorld=\u T(“你好,世界!”);

wcout目前我使用if/else,但有25个条件,效率极低。此外,我不允许使用第三方库。还有其他建议吗?如果你想使用那么糟糕的开关,你可能可以对输入参数进行散列,并对case语句使用预计算的散列。有一种更简单的方法:ATL conversion macros-T2A()OP试图将ASCII字符转换为数字。他从命令行的WCHAR*开始。他不太可能向控制台输入任何日语字符!