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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:24:01  来源:igfitidea点击:

What encoding does std::string.c_str() use?

c++stringutf-8

提问by Gili

I am trying to convert a C++ std::stringto UTF-8 or std::wstringwithout 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::wstringin a cross-platform fashion?

std::string.c_str()使用什么编码?如何将其转换为 UTF-8 或std::wstring跨平台方式?

回答by Alex Martelli

std::stringper 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::stringcontains 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 兼容。