Help me understand this URL decoder:
After playing around w开发者_如何学Goith 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.
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.
精彩评论