URI 转义 C++字符串
我正在寻找一种在 C++ 中进行 URI 转义的好方法,这对于跨平台项目来说是合理的。
我想要一个函数,它需要这样的字符串:
L"jiayou加油"
并返回:
L"jiayou%E5%8A%A0%E6%B2%B9"
我考虑使用类似 this 的东西,稍作修改即可使用 wchar_t。但是,这需要在 printf 调用之前从 utf-16 转换为 utf-8。这让我陷入了字符编码的地狱。
这种方法以及我研究过的所有其他方法都感觉是错误的方法。有没有一种好方法可以在 C++ 中转义 URI wstring?
I am looking for a good way to do a URI Escape in C++ that would be reasonable for a cross platform project.
I would like a function that would take a string like this:
L"jiayou加油"
And return:
L"jiayou%E5%8A%A0%E6%B2%B9"
I looked at using some thing like this, with minor modifacations to use wchar_t. However that would require converting from utf-16 to utf-8 before the printf call. This has lead me down character encoding hell.
This and all the other approaches I have looked into just feel like the wrong way. Is there a good way to URI Escape a wstring in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论你做什么,你都会陷入某种字符编码地狱(这就是字符编码的方式)。
来自 http://labs.apache.org/webarch/uri/rfc /rfc3986.html#字符:
因此,在某些时候,您需要将 URI 转换为适合您将 URI 发送到的目标的编码。如果是 UTF8,那么您最好在执行百分比编码之前进行转换,以便可以使用已经找到的库例程。如果它不是 UTF8 那么你需要知道 URI 的接收者期望什么(同样,这就是字符集编码的方式 - 你必须知道另一个人期望什么,或者能够告诉他),这样你就可以对期望的字符集中的字符进行百分比编码。
No matter what you do you're in some sort of character encoding hell (that's just the way it is with character encodings).
From http://labs.apache.org/webarch/uri/rfc/rfc3986.html#characters:
So, at some point you need to convert your URI to to the encoding that's appropriate to whatever you're sending the URI to. If that's UTF8 then you might as well do that conversion before you perform percent-encoding so you can use the library routine you've already found. If it's not UTF8 then you need to know what the recipient of the URI is expecting (again, that's the way it is with charset encodings - you have to know what the other guy is expecting, or be able to tell him) so you can percent-encode the characters in the character set encoding it's expecting.