What encoding does std::string.c_str() use?(std::string.c_str() 使用什么编码?)
问题描述
我正在尝试将 C++ std::string
转换为 UTF-8 或 std::wstring
而不丢失信息(考虑包含非 ASCII 字符的字符串).
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).
根据 http://forums.sun.com/thread.jspa?threadID=486770&forumID=31:
如果 std::string 包含非 ASCII 字符,您必须提供一个函数将您的编码转换为 UTF-8 [...]
If the std::string has non-ASCII characters, you must provide a function that converts from your encoding to UTF-8 [...]
std::string.c_str()
使用什么编码?如何以跨平台方式将其转换为 UTF-8 或 std::wstring
?
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
本身不使用编码——它将返回您放入其中的字节.例如,这些字节可能使用 ISO-8859-1 编码……或任何其他编码,实际上:关于编码的信息不存在——您必须知道这些字节来自哪里!
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.c_str() 使用什么编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::string.c_str() 使用什么编码?


基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01