【问题标题】:Wrong extended characters are represented表示错误的扩展字符
【发布时间】:2021-04-05 02:11:28
【问题描述】:

我找不到任何与我的问题相关的答案,我想要一个像“╔══╗”这样的字符串,但是用错误的字符代替它们。我发现了这个问题,那是因为有符号字符,并且每个字符的值与元素的 ascii 表值不同。如何在 C++ 中使用 char 来做到这一点?

int main() {
    char a[] = "╔══╗";
    std::cout << a << std::endl;
}

【问题讨论】:

  • 请显示minimal reproducible examplestd::cout &lt;&lt; "╔══════════════╗"; 按预期工作。
  • @churill 我改了谢谢
  • @Mahdi auto&amp; a = u8"╔══╗"; 是否也会产生错误的输出?

标签: c++ string char


【解决方案1】:

使用std::wstring(包含在&lt;string&gt;中)和L"..."

#include <string>

int main(/**/)
{
    std::wstring wstr = L"╔══════════════╗";
}

注意:没有很好的跨平台方式来打印这样的字符串。见How to print a wstring?How to print a wstring on MacOs or Unix?C++ wide character locale doesn't work

【讨论】:

  • 这会跳过“我如何在 c++ 中使用 char 来做到这一点?”部分问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多