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

发布于 2024-10-03 04:55:32 字数 406 浏览 0 评论 0原文

因此,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

[浮城] 2024-10-10 04:55:32

您也不能在 char* 上执行开关。 (但是当您实际需要将 WCHAR* 转换为 char* 时,请使用 WideCharToMultiByte)

您需要使用 if/else if 与 lstrcmpi, CompareString 或其他字符串比较函数。

或者,使用参数解析器库之一,例如 argtablegetopt

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

烟火散人牵绊 2024-10-10 04:55:32

我不确定这是否是一个好主意。 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*.

悲凉≈ 2024-10-10 04:55:32

尝试将其从 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

森林很绿却致人迷途 2024-10-10 04:55:32

这是我前段时间写的一个简单示例。

创建一个新的win32控制台应用程序并选择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;
}

当然,您不能打开字符串,但这应该为您提供将 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...

#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;
}

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文