协议缓冲区和 UTF-8
编码方案/多操作系统和 Endian-nes 的历史导致了对所有形式的字符串数据(即所有字母表)进行编码方面的混乱;因此,协议缓冲区仅处理其字符串类型中的 ASCII 或 UTF-8,并且我看不到任何接受 C++ wstring 的多态重载。那么问题是如何将 UTF-16 字符串获取到协议缓冲区中?
大概我需要在应用程序代码中将数据保留为 wstring,然后在将其填充到(或从中提取)消息之前执行 UTF-8 转换。最简单的 Windows/Linux 可移植方法是什么(来自受良好支持的库的单个函数调用将让我很高兴)?
数据将源自各种 Web 服务器(Linux 和 Windows),最终到达 SQL Server(以及可能的其他端点)。
-- 编辑 1--
Mark Wilkins 的建议似乎符合要求,也许有该库经验的人可以发布一个代码片段 -- 从 wstring 到 UTF-8 -- 这样我就可以衡量它有多容易。
-- 编辑 2 --
某物的建议更是如此。我将进一步研究增强序列化。
The history of Encoding Schemes / multiple Operating Systems and Endian-nes have led to a mess in terms of encoding all forms of string data (--i.e., all alphabets); for this reason protocol buffers only deals with ASCII or UTF-8 in its string types, and I can't see any polymorphic overloads that accept the C++ wstring. The question then is how is one expected to get a UTF-16 string into a protocol buffer ?
Presumably I need to keep the data as a wstring in my application code and then perform a UTF-8 conversion before I stuff it into (or extract from) the message. What is the simplest - Windows/Linux portable way to do this (A single function call from a well-supported library would make my day) ?
Data will originate from various web-servers (Linux and windows) and will eventually ends up in SQL Server (and possibly other end points).
-- edit 1--
Mark Wilkins suggestion seems to fit the bill, perhaps someone who has experience with the library can post a code snippet -- from wstring to UTF-8 -- so that I can gauge how easy it will be.
-- edit 2 --
sth's suggestion even more so. I will investigate boost serialization further.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Boost Serialization 库包含 UTF-8 codecvt 方面< /a> 可用于将 unicode 与 UTF-8 相互转换。文档中甚至有一个例子就是这样做的。
The Boost Serialization library contains a UTF-8 codecvt facet that you can use to convert unicode to UTF-8 and back. There even is an example in the documentation doing exactly that.
看一下 UTF8-CPP:
Take a look at UTF8-CPP:
这可能有点过分了,但是 ICU 库 可以满足您所需的一切,并且您可以在两者上使用它们Windows 和 Linux。
但是,如果您只想进行转换,那么在 Windows 下,只需调用
MultiByteToWideChar
和WideCharToMultiByte
即可完成 UTF-8 和UTF-16 之间的转换。例如:
对于 Linux,libidn 可能会满足您的需要。它可以在 UTF-8 和 UCS 之间进行转换,我认为这在某种程度上相当于 UTF-32。例如:
但是,在 Linux 中,我认为您最好只使用 UTF-8。除非您有 UTF-16 的现有库,否则我不确定是否有令人信服的理由在 Linux 中使用它。
It may be overkill, but the ICU libraries will do everything you need and you can use them on both Windows and Linux.
However, if you are only wanting conversion, then under Windows, a simple call to
MultiByteToWideChar
andWideCharToMultiByte
can do the conversion between UTF-8 andUTF-16. For example:
With Linux, libidn might do what you need. It can convert between UTF-8 and UCS, which I think is equivalent to UTF-32 at some level. For example:
However, in Linux I think you might be best simply working with UTF-8. Unless you have an existing library for UTF-16, I am not sure there is a compelling reason to use it in Linux.
在 Linux 上,这很简单:每个
wchar_t
都是一个 Unicode 代码点,通过简单的位元组,您可以找到相应的 UTF-8 字节。在 Windows 上,这并不难,因为有一个 API:WideCharToMultiByte(CP_UTF8, 0, input.c_str(), input.size(), &out[0], out.size() , 0,0);On Linux it's trivial: each
wchar_t
is one Unicode codepoint, and with trivial bitops you can find the corresponding UTF-8 byte(s). On Windows it isn't much harder, as there is an API for it:WideCharToMultiByte(CP_UTF8, 0, input.c_str(), input.size(), &out[0], out.size(), 0,0);