Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

URLEncode

, 25 Jun 2001
Convert string using URLEncode method
urlencode_demo.zip
URLEncode_demo
Release
urlencode.exe
urlencode.clw
urlencode.dsp
urlencode.dsw
urlencode.opt
urlencode.plg
// urlencode.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "urlencode.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

inline BYTE toHex(const BYTE &x)
{
	return x > 9 ? x + 55: x + 48;
}

CString URLEncode1(CString sIn)
{
	CString sOut;
	
	int k;
	const int nLen = sIn.GetLength() + 1;

	register LPBYTE pOutTmp = NULL;
	LPBYTE pOutBuf = NULL;
	register LPBYTE pInTmp = NULL;
	LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
	BYTE b = 0;

	//count not alphanumeric characters
	k = 0;
	
	pInTmp = pInBuf;
	while(*pInTmp)
	{
		if (!isalnum(*pInTmp) && !isalnum(*pInTmp))
			k++;
		pInTmp++;
	}

	//alloc out buffer
	pOutBuf = (LPBYTE)sOut.GetBuffer(nLen  + 2 * k);//new BYTE [nLen  + 3 * k];

	if(pOutBuf)
	{
		pInTmp	= pInBuf;
		pOutTmp = pOutBuf;
		
		// do encoding
		while (*pInTmp)
		{
			if(isalnum(*pInTmp))
				*pOutTmp++ = *pInTmp;
			else
				if(isspace(*pInTmp))
					*pOutTmp++ = '+';
				else
				{
					*pOutTmp++ = '%';
					*pOutTmp++ = toHex(*pInTmp>>4);
					*pOutTmp++ = toHex(*pInTmp%16);
				}
			pInTmp++;
		}
	
		*pOutTmp = '\0';
		//sOut=pOutBuf;
		//delete [] pOutBuf;
		sOut.ReleaseBuffer();
	}
	sIn.ReleaseBuffer();
	return sOut;
}

CString URLEncode2(CString sIn)
{
	CString sOut;
	
	const int nLen = sIn.GetLength() + 1;

	register LPBYTE pOutTmp = NULL;
	LPBYTE pOutBuf = NULL;
	register LPBYTE pInTmp = NULL;
	LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
	BYTE b = 0;
	
	//alloc out buffer
	pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3 - 2);//new BYTE [nLen  * 3];

	if(pOutBuf)
	{
		pInTmp	= pInBuf;
		pOutTmp = pOutBuf;
		
		// do encoding
		while (*pInTmp)
		{
			if(isalnum(*pInTmp))
				*pOutTmp++ = *pInTmp;
			else
				if(isspace(*pInTmp))
					*pOutTmp++ = '+';
				else
				{
					*pOutTmp++ = '%';
					*pOutTmp++ = toHex(*pInTmp>>4);
					*pOutTmp++ = toHex(*pInTmp%16);
				}
			pInTmp++;
		}
		*pOutTmp = '\0';
		//sOut=pOutBuf;
		//delete [] pOutBuf;
		sOut.ReleaseBuffer();
	}
	sIn.ReleaseBuffer();
	return sOut;
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		CString s = "\
<ROOT xmlns:sql=""urn:schemas-microsoft-com:xml-sql"" c=""000"">\
<T_PRODUCT n=""Usługi internetowe""><T_SALE q=""1"" p=""10.32"" \
w=""2001-06-20T15:27:25.047""/><T_SALE q=""1"" p=""10.32"" w=""2001\
-06-20T15:27:31.047""/><T_SALE q=""1"" p=""10.32"" w=""2001-06-20T1\
5:27:31.427""/><T_SALE q=""1"" p=""10.32"" w=""2001-06-20T15:27:31.\
627""/><T_SALE q=""1"" p=""10.32"" w=""2001-06-20T15:27:31.817""/><T\
_SALE q=""1"" p=""10.32"" w=""2001-06-20T15:27:31.997""/><T_SALE q=""\
1"" p=""10.32"" w=""2001-06-20T15:27:32.177""/><T_SALE q=""1"" p=""23.\
43"" w=""2001-06-21T11:49:57.940""/><T_SALE q=""1"" p=""23.43"" w=""20\
01-06-21T11:50:01.037""/><T_SALE q=""1"" p=""23.53"" w=""2001-06-21T1\
1:50:46.210""/><T_SALE q=""1"" p=""23.53"" w=""2001-06-21T11:50:46.22\
0""/><T_SALE q=""1"" p=""23.53"" w=""2001-06-21T11:50:46.220""/><T_SAL\
E q=""1"" p=""23.53"" w=""2001-06-21T11:50:46.220""/><T_SALE q=""1"" p=\
""23.53"" w=""2001-06-21T11:50:46.220""/><T_SALE q=""1"" p=""23.53"" w=\
""2001-06-21T11:50:46.220""/><T_SALE q=""1"" p=""23.53"" w=""2001-06-2\
1T11:50:46.220""/><T_SALE q=""1"" p=""23.53"" w=""2001-06-21T11:50:46.\
220""/><T_SALE q=""1"" p=""23.53"" w=""2001-06-21T11:50:46.230""/><T_SA\
LE q=""1"" p=""23.53"" w=""2001-06-21T11:50:46.230""/><T_SALE q=""1"" p=\
""23.53"" w=""2001-06-21T11:50:46.230""/><T_SALE q=""1"" p=""23.53"" w=""\
2001-06-21T11:50:46.230""/><T_SALE q=""1"" p=""23.53"" w=""2001-06-21\
T11:50:46.230""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:53.1\
50""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:53.150""/><\
T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:53.150""/><T_SALE \
q=""1"" p=""245.4"" w=""2001-06-21T11:50:53.150""/><T_SALE q=""1"" p\
=""245.4"" w=""2001-06-21T11:50:53.150""/><T_SALE q=""1"" p=""245.4\
"" w=""2001-06-21T11:50:53.150""/><T_SALE q=""1"" p=""245.4"" w=""20\
01-06-21T11:50:53.160""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-2\
1T11:50:53.160""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:\
53.160""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:53.1\
60""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:53.1\
60""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:53.1\
60""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:53.1\
60""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:50:54.6\
23""/><T_SALE q=""1"" p=""245.4"" w=""2001-06-21T11:5\
";

		DWORD x=GetTickCount();
		cout<<"String length: "<<s.GetLength()<<endl;
		for(int i = 0;i<10000;i++)
			URLEncode1(s);
		cout<<"URLEncode1 time: "<<(GetTickCount()-x)/1000.0<<endl;
		x = GetTickCount();
		for(i = 0;i<10000;i++)
			URLEncode2(s);
		cout<<"URLEncode2 time: "<<(GetTickCount()-x)/1000.0<<endl;

		if(URLEncode1(s) == URLEncode2(s))
			cout<<"comparision OK"<<endl;
		else
			cout<<"comparision ERROR"<<endl;
	}
	return nRetCode;
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Ryszard Krakowiak
Web Developer
Poland Poland
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 26 Jun 2001
Article Copyright 2001 by Ryszard Krakowiak
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid