协议缓冲区和 UTF-8

发布于 2024-08-18 20:10:29 字数 508 浏览 2 评论 0原文

编码方案/多操作系统和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琉璃梦幻 2024-08-25 20:10:29

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.

娇纵 2024-08-25 20:10:29

看一下 UTF8-CPP

// converts a utf-8 encoded std::string s to utf-16 wstring ws
utf8to16(s.begin(), s.end(), back_inserter(ws));

Take a look at UTF8-CPP:

// converts a utf-8 encoded std::string s to utf-16 wstring ws
utf8to16(s.begin(), s.end(), back_inserter(ws));
甜是你 2024-08-25 20:10:29

这可能有点过分了,但是 ICU 库 可以满足您所需的一切,并且您可以在两者上使用它们Windows 和 Linux。

但是,如果您只想进行转换,那么在 Windows 下,只需调用 MultiByteToWideCharWideCharToMultiByte 即可完成 UTF-8 和

UTF-16 之间的转换。例如:

// utf-8 to utf-16
MultiByteToWideChar( CP_UTF8, 0, myUtf8String, -1,
                     myUtf16Buf, lengthOfUtf16Buf );

对于 Linux,libidn 可能会满足您的需要。它可以在 UTF-8 和 UCS 之间进行转换,我认为这在某种程度上相当于 UTF-32。例如:

// utf-8 to UCS
ucsStr = stringprep_utf8_to_ucs4( "asdf", 4, &items );

但是,在 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 and WideCharToMultiByte can do the conversion between UTF-8 and

UTF-16. For example:

// utf-8 to utf-16
MultiByteToWideChar( CP_UTF8, 0, myUtf8String, -1,
                     myUtf16Buf, lengthOfUtf16Buf );

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:

// utf-8 to UCS
ucsStr = stringprep_utf8_to_ucs4( "asdf", 4, &items );

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.

想挽留 2024-08-25 20:10:29

在 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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文