字符串、u16string 和 u16string 之间的转换u32字符串
我一直在寻找一种在 Unicode 字符串类型之间进行转换的方法,并遇到了 此方法。我不仅不完全理解该方法(没有评论),而且该文章暗示将来会有更好的方法。
如果这是最好的方法,您能否指出它的工作原理,如果不是,我想听到更好方法的建议。
I've been looking for a way to convert between the Unicode string types and came across this method. Not only do I not completely understand the method (there are no comments) but also the article implies that in future there will be better methods.
If this is the best method, could you please point out what makes it work, and if not I would like to hear suggestions for better methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
mbstowcs()
和wcstombs()
不一定转换为 UTF-16 或 UTF-32,它们会转换为wchar_t
以及任何语言环境wchar_t
编码是。所有 Windows 语言环境都使用两字节wchar_t
和 UTF-16 作为编码,但其他主要平台使用 4 字节wchar_t
和 UTF-32(甚至非-某些语言环境的 Unicode 编码)。仅支持单字节编码的平台甚至可以有一个单字节wchar_t
并且编码因区域设置而异。因此,在我看来,对于可移植性和 Unicode 来说,wchar_t 似乎是一个糟糕的选择。 *C++11中引入了一些更好的选项; std::codecvt 的新专业化、新的 codecvt 类和新模板使使用它们进行转换非常方便。
首先,使用 codecvt 的新模板类是 std::wstring_convert。创建 std::wstring_convert 类的实例后,您可以轻松地在字符串之间进行转换:
为了进行不同的转换,您只需要不同的模板参数,其中之一是 codecvt 方面。以下是一些易于与 wstring_convert 一起使用的新方面:
使用这些方面的示例:
新的 std::codecvt 专业化有点难以使用,因为它们具有受保护的析构函数。为了解决这个问题,您可以定义一个具有析构函数的子类,或者您可以使用 std::use_facet 模板函数来获取现有的 codecvt 实例。此外,这些专业化的一个问题是您无法在 Visual Studio 2010 中使用它们,因为模板专业化不适用于 typedef 类型,并且编译器将 char16_t 和 char32_t 定义为 typedef。下面是定义您自己的 codecvt 子类的示例:
char16_t 专业化在 UTF-16 和 UTF-8 之间进行转换。 char32_t 专业化、UTF-32 和 UTF-8。
请注意,C++11 提供的这些新转换不包括任何在 UTF-32 和 UTF-16 之间直接转换的方法。相反,您只需组合 std::wstring_convert 的两个实例即可。
***** 我想我应该添加一个关于 wchar_t 及其用途的注释,以强调为什么它通常不应该用于 Unicode 或可移植国际化代码。以下是我的回答的简短版本 https://stackoverflow.com/a/11107667/365496
什么是 wchar_t?
wchar_t 的定义使得任何语言环境的 char 编码都可以转换为 wchar_t,其中每个 wchar_t 恰好代表一个代码点:
这不要求 wchar_t 足够大以同时表示所有语言环境中的任何字符。也就是说,用于 wchar_t 的编码可能因语言环境而异。这意味着您不一定使用一种语言环境将字符串转换为 wchar_t,然后使用另一种语言环境转换回 char。
由于这似乎是 wchar_t 在实践中的主要用途,您可能想知道如果不是的话它有什么好处。
wchar_t 的最初意图和目的是通过定义它来简化文本处理,使得它需要从字符串的代码单元到文本的字符的一对一映射,从而允许使用与 ascii 字符串相同的简单算法与其他语言一起工作。
不幸的是,对 wchar_t 的要求假设字符和代码点之间存在一对一的映射来实现这一点。 Unicode 打破了这一假设,因此您也不能安全地将 wchar_t 用于简单文本算法。
这意味着便携式软件不能使用 wchar_t 作为语言环境之间文本的通用表示形式,也不能使用简单的文本算法。
今天 wchar_t 有什么用?
无论如何,对于可移植代码来说并不多。如果定义了 __STDC_ISO_10646__ ,则 wchar_t 的值直接表示在所有区域设置中具有相同值的 Unicode 代码点。这使得前面提到的区域间转换变得安全。但是,您不能仅依靠它来决定是否可以以这种方式使用 wchar_t,因为虽然大多数 UNIX 平台都定义了它,但 Windows 却没有定义它,即使 Windows 在所有语言环境中使用相同的 wchar_t 语言环境也是如此。
Windows没有定义
__STDC_ISO_10646__
的原因我认为是因为Windows使用UTF-16作为其wchar_t编码,并且因为UTF-16使用代理对来表示大于U+FFFF的码点,这意味着UTF -16 不满足__STDC_ISO_10646__
的要求。对于特定于平台的代码 wchar_t 可能更有用。它在 Windows 上基本上是必需的(例如,如果不使用 wchar_t 文件名,某些文件根本无法打开),尽管据我所知,Windows 是唯一一个如此的平台(所以也许我们可以将 wchar_t 视为“Windows_char_t”)。
事后看来,wchar_t 显然对于简化文本处理或作为与区域设置无关的文本的存储没有用处。可移植代码不应尝试将其用于这些目的。
mbstowcs()
andwcstombs()
don't necessarily convert to UTF-16 or UTF-32, they convert towchar_t
and whatever the localewchar_t
encoding is. All Windows locales uses a two bytewchar_t
and UTF-16 as the encoding, but the other major platforms use a 4-bytewchar_t
with UTF-32 (or even a non-Unicode encoding for some locales). A platform that only supports single-byte encodings could even have a one bytewchar_t
and have the encoding differ by locale. Sowchar_t
seems to me to be a bad choice for portability and Unicode. *Some better options have been introduced in C++11; new specializations of std::codecvt, new codecvt classes, and a new template to make using them for conversions very convienent.
First the new template class for using codecvt is std::wstring_convert. Once you've created an instance of a std::wstring_convert class you can easily convert between strings:
In order to do different conversion you just need different template parameters, one of which is a codecvt facet. Here are some new facets that are easy to use with wstring_convert:
Examples of using these:
The new std::codecvt specializations are a bit harder to use because they have a protected destructor. To get around that you can define a subclass that has a destructor, or you can use the std::use_facet template function to get an existing codecvt instance. Also, an issue with these specializations is you can't use them in Visual Studio 2010 because template specialization doesn't work with typedef'd types and that compiler defines char16_t and char32_t as typedefs. Here's an example of defining your own subclass of codecvt:
The char16_t specialization converts between UTF-16 and UTF-8. The char32_t specialization, UTF-32 and UTF-8.
Note that these new conversions provided by C++11 don't include any way to convert directly between UTF-32 and UTF-16. Instead you just have to combine two instances of std::wstring_convert.
***** I thought I'd add a note on wchar_t and its purpose, to emphasize why it should not generally be used for Unicode or portable internationalized code. The following is a short version of my answer https://stackoverflow.com/a/11107667/365496
What is wchar_t?
wchar_t is defined such that any locale's char encoding can be converted to wchar_t where every wchar_t represents exactly one codepoint:
This does not require that wchar_t be large enough to represent any character from all locales simultaneously. That is, the encoding used for wchar_t may differ between locales. Which means that you cannot necessarily convert a string to wchar_t using one locale and then convert back to char using another locale.
Since that seems to be the primary use in practice for wchar_t you might wonder what it's good for if not that.
The original intent and purpose of wchar_t was to make text processing simple by defining it such that it requires a one-to-one mapping from a string's code-units to the text's characters, thus allowing the use of same simple algorithms used with ascii strings to work with other languages.
Unfortunately the requirements on wchar_t assume a one-to-one mapping between characters and codepoints to achieve this. Unicode breaks that assumption, so you can't safely use wchar_t for simple text algorithms either.
This means that portable software cannot use wchar_t either as a common representation for text between locales, or to enable the use of simple text algorithms.
What use is wchar_t today?
Not much, for portable code anyway. If
__STDC_ISO_10646__
is defined then values of wchar_t directly represent Unicode codepoints with the same values in all locales. That makes it safe to do the inter-locale conversions mentioned earlier. However you can't rely only on it to decide that you can use wchar_t this way because, while most unix platforms define it, Windows does not even though Windows uses the same wchar_t locale in all locales.The reason Windows doesn't define
__STDC_ISO_10646__
I think is because Windows use UTF-16 as its wchar_t encoding, and because UTF-16 uses surrogate pairs to represent codepoints greater than U+FFFF, which means that UTF-16 doesn't satisfy the requirements for__STDC_ISO_10646__
.For platform specific code wchar_t may be more useful. It's essentially required on Windows (e.g., some files simply cannot be opened without using wchar_t filenames), though Windows is the only platform where this is true as far as I know (so maybe we can think of wchar_t as 'Windows_char_t').
In hindsight wchar_t is clearly not useful for simplifying text handling, or as storage for locale independent text. Portable code should not attempt to use it for these purposes.
我已经编写了辅助函数来与 UTF8 字符串相互转换 (C++11):
使用示例:
I've written helper functions to convert to/from UTF8 strings (C++11):
Usage example:
据我所知,C++ 没有提供从 UTF-32 到 UTF-32 的转换的标准方法。然而,对于 UTF-16,有方法 mbstowcs(多字节到宽字符串),以及相反的方法 wcstombs。
如果您也需要 UTF-32,则需要 iconv,它在 POSIX 2001 中,但不在标准 C 中,因此在 Windows 上,您需要像 libiconv 这样的替代品。
以下是如何使用 mbstowcs 的示例:
相反的情况如下:
Nitpick: 是的,我知道,wchar_t 的大小是实现定义的,因此它可以 为 4 字节 (UTF-32)。但是,我不知道有哪个编译器可以做到这一点。
As far as I know, C++ provides no standard methods to convert from or to UTF-32. However, for UTF-16 there are the methods mbstowcs (Multi-Byte to Wide character string), and the inverse, wcstombs.
If you need UTF-32 too, you need iconv, which is in POSIX 2001 but not in standard C, so on Windows you'll need a replacement like libiconv.
Here's an example on how to use mbstowcs:
The reverse goes like this:
Nitpick: Yes, I know, the size of wchar_t is implementation defined, so it could be 4 Bytes (UTF-32). However, I don't know a compiler which does that.