无法使用 swprintf 在 WCHAR* 中打印字符*

Can't print char* in WCHAR* with swprintf

本文关键字:打印 字符 WCHAR swprintf      更新时间:2023-10-16

我有那个有效的函数(我只需要在最后将 wstring 转到 WHAR*(:

wstring stringtowstring(string str)
{
WCHAR ch[256];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)ch, 256);
return wstring(ch);
}

但我想知道为什么没有:

WCHAR tmp[256];
swprintf(tmp, L"%s", "test");

假设Visual Studio在上下文中,这是swprintf记录的非标准行为。

使用 c 和 s指定的字符和字符串参数printf 系列函数解释为 char 和 char*,或由 wprintf 系列函数解释为wchar_t 和 wchar_t*。使用 C 和 S 指定的字符和字符串参数由 printf 系列函数解释为 wchar_t 和 wchar_t*,或由 wprintf 系列函数解释为 char 和 char*。此行为特定于Microsoft。

Microsoft特异性: Z 类型字符以及 c、C、s 和 S 类型字符与 printf 和 wprintf 函数一起使用时的行为是Microsoft 扩展。ISO C 标准在所有格式化函数中一致地对窄字符和字符串使用 c 和 s,对宽字符和字符串使用 C 和 S。