从扩展 ascii 转换为 utf8

发布于 2024-09-18 19:56:01 字数 145 浏览 5 评论 0原文

如何使用 Microsoft Visual Studio 2005 将扩展 ascii 编码的 std::string 转换为 utf8?

我正在使用谷歌协议缓冲区,如果我在没有转换的情况下给出它,它会抱怨我的字符串中的非 utf8 字符,这是真的......

How do you convert an std::string encoded in extended ascii to utf8 using microsoft visual studio 2005?

I'm using google protocol buffer and it's complaining about non utf8 characters in my string if I give it without conversion, which is true...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

二货你真萌 2024-09-25 19:56:01

使用 MultiByteToWideChar 将字符串转换为 UTF -16,然后使用 WideCharToMultiByte 进行转换转为 UTF-8。

Use MultiByteToWideChar to convert your string to UTF-16, then use WideCharToMultiByte to convert it to UTF-8.

牵你手 2024-09-25 19:56:01

我们假设神秘的扩展 ASCII 就是 Latin1。然后使用维基百科中的掩码:

110yyyxx 10xx xxxx

由于您只有 00..FF 那么您就有:1100 00xx 10xx xxxx

如果字符代码<<,则转换算法如下: 127然后就按原样转储它,如果它> 127 那么你就做0xC0 | ((x & 0xC0) >> 24) 转到第一个字节,第二个是 ((x & 0x3F) | 0x80)

Let's assume that mysterious Exntended ASCII is just Latin1. Then use mask from wikipedia:

110y yyxx 10xx xxxx

Since you have only 00..FF then you have: 1100 00xx 10xx xxxx.

Conversion algorithm will be following, if char code is < 127 then just dump it as is, if it is > 127 then you do 0xC0 | ((x & 0xC0) >> 24) goes to first byte, second is ((x & 0x3F) | 0x80)

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