繁体   English   中英

如何在Linux的wstring中存储unicode字符?

[英]How to store unicode character in wstring on linux?

#include <iostream>
using namespace std;

int main() {
    std::wstring str  = L"\u00A2";
    std::wcout << str;  
    return 0;
}

为什么这不起作用? 以及如何解决呢?

它不起作用,因为在默认的C语言环境中,没有对应于U + 00A2的字符。

如果您使用标准的ubuntu安装,则您的用户语言环境很有可能使用比US-ASCII更大的字符集,很有可能使用UTF-8进行Unicode编码。 因此,您只需要切换到环境中指定的语言环境,如下所示:

#include <iostream>
/* locale is needed for std::setlocale */
#include <locale>
#include <string>

int main() {
  /* The following switches to the locale specified
   * by the LC_ALL environment variable.
   */
  std::setlocale (LC_ALL, "");
  std::wstring str  = L"\u00A2";
  std::wcout << str;  
  return 0;
}

如果使用std::string而不是std::wstringstd::cout而不是std::wcout ,则不需要setlocale因为不需要翻译(前提是控制台需要UTF-8)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM