C++、WCHAR[] 到 std::cout 和比较

发布于 2024-08-08 12:14:05 字数 258 浏览 3 评论 0原文

我需要将 WCHAR[] 放入 std::cout ...它是从 Native Wifi API 回调传递的 PWLAN_CONNECTION_NOTIFICATION_DATA 的一部分。

我尝试了简单的 std::cout <<变量;但它打印出第一个字符的数字地址。比较 (var == L"some text") 也不起作用。调试器返回预期值,但比较返回 0。如何将此数组转换为标准字符串(std::string)?

提前致谢

I need to put WCHAR[] to std::cout ... It is a part of PWLAN_CONNECTION_NOTIFICATION_DATA passed from Native Wifi API callback.

I tried simply std::cout << var; but it prints out the numeric address of first char. the comparision (var == L"some text") doesn't work either. The debugger returns the expected value, however the comparision returns 0. How can I convert this array to a standard string(std::string)?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

困倦 2024-08-15 12:14:05

一些解决方案:

  • 写入 std::wcout 而不是
  • 转换:
    • 标准方式,使用 std::codecvt
    • Win32 方式,使用 WideCharToMultibyte

Some solutions:

  • Write to std::wcout instead
  • Convert:
    • The standard way, using std::codecvt
    • The Win32 way, using WideCharToMultibyte
简美 2024-08-15 12:14:05

要打印到 cout,您应该使用 std::wcout

至于比较,我不太明白你的意思。

  • 如果 varwchar_t[],那么您正在比较两个指针。结果很可能是假的,因为虽然字符串内容可能相同,但它们在物理上分配在不同的内存位置。答案是使用类似 strcmp 的函数来比较 C 样式字符串(字符指针),或者使用 C++ 字符串类。
  • 并且operator==通常返回一个bool,而不是一个整数。因此它可以返回 false,但不能返回 0...除非您自己创建了一些奇怪的重载。 (只有当 var 是用户定义的类型时才有可能。

For printing to cout, you should use std::wcout instead.

As for the comparison, I'm not really sure what you mean.

  • if var is a wchar_t[], then you are comparing two pointers. And the result will most likely be false, because while the string contents may be the same, they are physically allocated in different memory locations. The answer is to either use a function like strcmp which compares C-style strings (char pointers), or to use the C++ string class.
  • and the operator== usually returns a bool, not an integer. So it can return false, but it can't return 0... Unless you've created some weird overload yourself. (and that is only possible if var is a user-defined type.
吹梦到西洲 2024-08-15 12:14:05

假设 var 是一个 wchar_t *var == L"some text" 进行指针比较。为了比较 var 指向的字符串,请使用诸如 wcscmp 之类的函数。

Assuming var is a wchar_t *, var == L"some text" does a pointer comparison. In order to compare the string pointed to by var, use a function such as wcscmp.

·深蓝 2024-08-15 12:14:05

使用以下内容

#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif

use the following

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