Click here to Skip to main content
Click here to Skip to main content

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

By , 17 Jul 2007
 

Introduction

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!

Background

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("http://www.google.com/search?q=my search"));
// This will result in http://www.google.com/search?q=my%20search
cEncoder.Decode(_T("http://www.google.com/search?q=%22my%20search%22"));
// This will result in http://www.google.com/search?q="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.

History

  • 17th July, 2007: Initial post.

License

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAdditional reserverd characters PinmemberFernando Sarlenga25-Jul-07 5:23 
GeneralRe: Additional reserverd characters PinmemberDaniel Cohen Gindi25-Jul-07 7:34 

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 17 Jul 2007
Article Copyright 2007 by Daniel Cohen Gindi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid