从扩展 ascii 转换为 utf8
如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 MultiByteToWideChar 将字符串转换为 UTF -16,然后使用 WideCharToMultiByte 进行转换转为 UTF-8。
Use MultiByteToWideChar to convert your string to UTF-16, then use WideCharToMultiByte to convert it to UTF-8.
我们假设神秘的扩展 ASCII 就是 Latin1。然后使用维基百科中的掩码:
由于您只有 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:
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)