Click here to Skip to main content
14,447,818 members

How to encode/decode URLs to the UTF8 format (with %20 and so)

Rate this:
3.70 (8 votes)
Please Sign up or sign in to vote.
3.70 (8 votes)
17 Jul 2007CPOL
This article shows how to encode/decode URLs to the UTF8 format (with %20 and so) if you have to have web support in your application.


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 a 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:

CUrlEncode cEncoder;
cEncoder.Encode(_T(" search"));
// This will result in
// This will result in"my search"

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/*=FALSE*/);
CString Decode(CString strURL);

Here, 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 TRUE.

That's about it, hope I helped.


  • 17th July, 2007: Initial post.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Daniel Cohen Gindi
Software Developer (Senior)
Israel Israel
No Biography provided

Comments and Discussions

QuestionSolution for URL Decode (Including Japanese Characters) Pin
Prashant Kawade8-Sep-15 21:14
MemberPrashant Kawade8-Sep-15 21:14 
QuestionGood work, thanks Pin
bosfan24-Oct-14 1:54
Memberbosfan24-Oct-14 1:54 
GeneralThank YOU! Pin
djonik156216-Jun-11 22:54
Memberdjonik156216-Jun-11 22:54 
GeneralMy vote of 5 Pin
Blood_Axis15-Sep-10 12:13
MemberBlood_Axis15-Sep-10 12:13 
QuestionHow I can use your code to convert char* UTF-8 to wchar string(decode in java) Pin
Member 327298422-Mar-09 6:20
MemberMember 327298422-Mar-09 6:20 
GeneralSome easier way to encode and decode the URLs [modified] Pin
elo12331-Dec-08 0:45
Memberelo12331-Dec-08 0:45 
GeneralKorean/Chinese/Japanese support Pin
Taein Kim4-Sep-07 4:53
MemberTaein Kim4-Sep-07 4:53 
GeneralRe: Korean/Chinese/Japanese support Pin
Kenny Zhao29-Sep-07 21:04
MemberKenny Zhao29-Sep-07 21:04 
GeneralRe: Korean/Chinese/Japanese support Pin
chris_cppteam26-Nov-08 16:12
Memberchris_cppteam26-Nov-08 16:12 
GeneralRe: Korean/Chinese/Japanese support Pin
Never Winter20-Jul-08 20:04
MemberNever Winter20-Jul-08 20:04 
GeneralRe: Korean/Chinese/Japanese support Pin
Daniel Cohen Gindi20-Jul-08 21:58
MemberDaniel Cohen Gindi20-Jul-08 21:58 
GeneralAdditional reserverd characters Pin
Fernando Sarlenga25-Jul-07 6:23
MemberFernando Sarlenga25-Jul-07 6:23 
GeneralRe: Additional reserverd characters Pin
Daniel Cohen Gindi25-Jul-07 8:34
MemberDaniel Cohen Gindi25-Jul-07 8:34 
GeneralDecoding [modified] Pin
Daniel Cohen Gindi17-Jul-07 22:03
MemberDaniel Cohen Gindi17-Jul-07 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 17 Jul 2007


25 bookmarked