C++ std::string.c_str() 使用什么编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1010783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What encoding does std::string.c_str() use?
提问by Gili
I am trying to convert a C++ std::string
to UTF-8 or std::wstring
without losing information (consider a string that contains non-ASCII characters).
我正在尝试将 C++ 转换std::string
为 UTF-8 或std::wstring
不丢失信息(考虑包含非 ASCII 字符的字符串)。
According to http://forums.sun.com/thread.jspa?threadID=486770&forumID=31:
根据http://forums.sun.com/thread.jspa?threadID=486770&forumID=31:
If the std::string has non-ASCII characters, you must provide a function that converts from your encoding to UTF-8 [...]
如果 std::string 具有非 ASCII 字符,则必须提供一个函数将编码转换为 UTF-8 [...]
What encoding does std::string.c_str()
use? How can I convert it to UTF-8 or std::wstring
in a cross-platform fashion?
std::string.c_str()
使用什么编码?如何将其转换为 UTF-8 或std::wstring
跨平台方式?
回答by Alex Martelli
std::string
per se uses no encoding -- it will return the bytes you put in it. For example, those bytes might be using ISO-8859-1 encoding... or any other, really: the information about the encoding is just not there -- you have to know where the bytes were coming from!
std::string
本身不使用编码——它将返回您放入其中的字节。例如,这些字节可能使用 ISO-8859-1 编码……或任何其他编码,实际上:关于编码的信息不存在——您必须知道字节来自哪里!
回答by Naaff
std::string
contains any sequence of bytes, so the encoding is up to you. You must know how it is encoded. However, if you don't know that it is something else, it's probably just ASCII. In which case, it's already UTF-8 compatible.
std::string
包含任何字节序列,因此编码取决于您。你必须知道它是如何编码的。然而,如果你不知道它是别的东西,它可能只是 ASCII。在这种情况下,它已经与 UTF-8 兼容。