在 fmt lib C++ 中显示 unicode 字符

Ali*_*ine 5 c++ unicode fmt

我想显示\xe2\x88\x9e具有 Unicode 的无穷大符号U+221E。我目前正在使用fmt库,它应该有很多支持并且是跨平台的。

\n
fmt::print("", fmt::styled("\xe2\x88\x9e >", fmt::emphasis::bold | fg(fmt::color::aquamarine)));\n
Run Code Online (Sandbox Code Playgroud)\n

我得到以下输出:

\n
? >\n
Run Code Online (Sandbox Code Playgroud)\n

我也尝试过设置:setlocale(LC_ALL, "en_US.UTF-8");没有帮助。我使用的是 Windows 11 x64。

\n

警告:

\n
warning C4566: character represented by universal-character-name \'\\u221E\' cannot be represented in the current code page (1252)\n
Run Code Online (Sandbox Code Playgroud)\n

MS Visual Studio 2022 IDE。

\n

我应该在项目属性中更改字符集吗?当前设置为:使用 Unicode 字符集,第二个选项是:使用多字节字符集

\n

vit*_*aut 2

为此,您需要使用该/utf-8标志进行编译。除此之外,这会将字符串文字编码设置为 UTF-8。它由 {fmt} 检测到,它使用 Unicode API 写入控制台。一般来说,更改区域设置没有帮助,因为问题出在其他地方:

  1. 您的字符串文字编码(有时称为执行编码)与您的源编码不匹配 - 这就是导致警告的原因。
  2. 控制台使用单独的代码页(编码)。

出于类似的原因,使用宽字符串也无济于事。