如何将 WCHAR * 转换为常规字符串?
因此,在 Win32 API 中,我的 main 函数是这样定义的:
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* 上执行 switch case。
感谢您的关注。
So in Win32 API, I have my main function defined thus:
wmain(int argc, WCHAR* argv[])
I'm passing some arguments to it, and I'd like to execute a switch case based on the value of the argument, something like this.
wmain(int argc, WCHAR* argv[])
{
char* temp = argv[];
switch (temp) {
case "one": blah blah;
...
}
Of course, the temp=argv[] doesn't work, I'm looking for a suggestion to convert it. Right now I have an if-else-if thing going on, and it's VERY inefficient!
The reason I need to convert it is because I cannot execute a switch case on a WCHAR*.
Thanks for looking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您也不能在 char* 上执行开关。 (但是当您实际需要将 WCHAR* 转换为 char* 时,请使用 WideCharToMultiByte)
您需要使用 if/else if 与 lstrcmpi, CompareString 或其他字符串比较函数。
或者,使用参数解析器库之一,例如 argtable 或 getopt
You can't execute a switch on a char* either. (But when you actually need to convert WCHAR* to char*, use WideCharToMultiByte)
You need to use if/else if with lstrcmpi, CompareString or some other string compare function.
Alternatively, use one of the parameter parser libraries like argtable or getopt
我不确定这是否是一个好主意。 WCHAR* 可能包含无法以有意义的方式映射到 char* 的 unicode 字符。如果您想忽略这一点,可以在 http://www 上找到一个论坛帖子.codeguru.com/forum/showthread.php?t=336106 其中提供了一些从 WCHAR* 转换为 char* 的建议。
I am not sure if this is a good idea to do. A WCHAR* could contain unicode characters which cannot be mapped to a char* in a meaningful way. In case you want to ignore this, there is a forum post at http://www.codeguru.com/forum/showthread.php?t=336106 which has some suggestions for converting from WCHAR* to char*.
尝试将其从 std::wstring 转换为 std::string,这很简单,也许还有更短的方法。
使用 std::wstring 构造函数将 WCHAR* 转换为 std::wstring,然后使用 std::wstring 方法之一转换为 std::String
Try converting it from std::wstring to std::string, its easy, maybe there is a shorter way.
Convert WCHAR* to std::wstring using std::wstring constractor, and then use one of std::wstring method to convert to std::String
这是我前段时间写的一个简单示例。
创建一个新的win32控制台应用程序并选择ATL支持。添加这个并编译/运行...
当然,您不能打开字符串,但这应该为您提供将 WCHAR 读入字符所需的信息。从那里,你可以很容易地转换为 int 。
希望这有帮助;)
Here's a quick example I wrote some time ago.
Create a new win32 console application and select ATL support. Add this and compile/run...
Of course, you can't switch on a string, but this should give you the info you need in order to read a WCHAR into a char. From there, you can convert to int easily enough..
Hope this helps ;)