This article will show you how to encode/decode URLs to the UTF-8 format. If you are writing an application that must have web support, and for example navigating
WebBrowser ActiveX control to a certain URL, you have to encode it, for there are many characters (e.g., Hebrew, accented Latin, spaces, and so on...) that cannot be in a URL.
I have written a class to do all the work, and it is the simplest to use. Enjoy!
URLs support only about 60 characters, and all other characters are written in the UTF-8 format, using the %XX hexadecimal format.
For more information about the main rules of URL encoding, you can have a look here.
Using the Code
I have included the source code in this article, and you can use it without any effort:
This class can deal with much more than spaces, and this is just a simple example.
The usage for the functions is as follows:
CString Encode(CString strURL, BOOL bEncodeReserved);
CString Decode(CString strURL);
bEncodeReserved means that you want to encode the reserved characters too. This parameter is dangerous for full URLs because it will also encode
characters like '/', and will destroy your URL. But if you are encoding keywords, for example, you should set this parameter to
That's about it, hope I helped.
- 17th July, 2007: Initial post.