帮助我理解这个 URL 解码器:

发布于 2024-11-09 03:39:31 字数 653 浏览 6 评论 0原文

在自己尝试了 URL 解码之后,我设法想出了一些可行的想法 - 但它们效率不高。由于 URL 解码是我的程序中可能出现严重瓶颈的地方,因此我决定在互联网上寻找更有效的解决方案。我遇到了这篇 Codeguru 文章:

http://www.codeguru。 com/cpp/cpp/string/conversions/article.php/c12759

现在,我并不反对使用别人的代码,特别是如果它比我的更好。然而,我想在这样做之前知道它是如何工作的。使用您不完全理解的代码是没有意义的。

这就是我陷入困境的地方,我了解解码函数中进行的大部分指针工作。我迷失的是 HEX2DEC 数组和相关的转换算法。它没有在网站上提供,而是在示例下载中提供,因此为了方便起见,我将其与解码功能一起发布在此处:

http://pastebin.com/AVDnr6cK

这个函数到底是如何使用这个数组将十六进制转换为十进制,然后将十进制转换为它的 ASCII 等效值?

再次感谢您的帮助。

After playing around with URL decoding myself, I managed to come up with some ideas that worked - but they weren't very efficient. Since URL decoding is a place where severe bottlenecking could occur in my program I decided to go out on the internet and find a more efficient solution. I ran across this codeguru article:

http://www.codeguru.com/cpp/cpp/string/conversions/article.php/c12759

Now, I'm not against using someone else's code, especially if it's better than mine. However, I like to know how it works before I do. No point in using code you don't fully understand.

Here is where I'm stuck, I understand most of the pointer work going on in the decoding function. Where I get lost is the HEX2DEC array and the associated conversion arithmetic. It's not provided on the website, but rather in the example download so I'll post it here for your convenience along with the decoding function:

http://pastebin.com/AVDnr6cK

How exactly is this function using this array to convert the hexadecimal to decimal, and then the decimal into it's ASCII equivalent?

Thank you again for your help.

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

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

发布评论

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

评论(1

神回复 2024-11-16 03:39:31

该数组是一个查找表,对于用作索引的每个 ascii 字符,如果该字符不是十六进制字符(在 [0..9][A ..F][a..f])或转换为该十六进制字符的整数。

在主循环中,每当找到 % 时,首先使用数组来确定后面的两个字符是否为十六进制 (!= -1),如果是,则将其转换为整个序列。运算(dec1 << 4) + dec2相当于dec1 * 16 + dec2,表示将两个十六进制字符转换为8位整数。

The array is a lookup table, for each ascii character used as an index it will provide either -1 if the character is not an hexadecimal character (in [0..9], [A..F] or [a..f]) or the conversion to integer of that hex character.

In the main loop, whenever a % is found the array is first used to determine whether the following two characters are hexadecimal (!= -1) and if so it converts the whole sequence. The operation (dec1 << 4) + dec2 is equivalent to dec1 * 16 + dec2, and represents the conversion to an integer of 8 bits of the two hex chars.

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