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

URLEncode

By , 25 Jun 2001
 
<!-- Download Links --> <!-- Add the rest of your HTML here -->

URLEncode

I use this functions to prepare POST strings with XML data. The first function URLEncode1 uses less memory but is slower than the second URLEncode2. Both functions return a CString and get a CString as the input parameter.

The demo project contains sample usage with execution time of presented functions.

Source

Helper function

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

URLEncode1

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;
}

URLEncode2

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;
}

Modifications

26.06.2001 - changed out buffer memory allocation (thx 2 Marc Brooks and Matthias)

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

About the Author

Ryszard Krakowiak
Web Developer
Poland Poland
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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThis is slightly broke, and to clear up some confussion [modified]memberKevinSW21-Sep-12 1:35 
GeneralHere's a much simpler method that supports Unicodememberdc_200025-Apr-11 0:59 
I understand that this article is somewhat old. For those who'd like to have a full support for Unicode you can use this:
 
(And it is also much more easy to understand. There's no need to scrimp on memory and speed in this case.)
 
CString EscapeURL(LPCTSTR pStrURL)
{
	//Escape characters in 'pStrURL' to be inseted into URL
	//RETURN:
	//		= Escaped URL string
	ASSERT(pStrURL);
	CString strOut;
 
	int nLn = lstrlen(pStrURL);
	for(int i = 0; i < nLn; i++)
	{
		TCHAR z = pStrURL[i];
		if(z == '\\')
		{
			strOut += _T('/');
		}
		else if(z == '/' ||    //There's no need to escape everything indiscriminately
			z == ':' ||
			z == '.' ||
			z == '_' ||
			(z < 0x100 && _istalnum(z)))
		{
			strOut += z;
		}
		else if(z < 0x100)
		{
			strOut.AppendFormat(_T("%%%x%x"), 
				(BYTE)(z >> 4),
				(BYTE)(z & 0xf));
		}
		else
		{
			ASSERT(z >= 0x100);
			strOut.AppendFormat(_T("&#%u;"), 
				z);
		}
	}
 
	return strOut;
}

Questionwtf is "if (!isalnum(*pInTmp) && !isalnum(*pInTmp))" ?members98769026-Jul-07 23:34 
GeneralURLEncode2() Unicode versionmembermcanti2-May-06 2:28 
GeneralUnicode solutionmemberangelo moscati4-Jul-05 3:41 
GeneralRe: Unicode solutionmemberangelo moscati4-Jul-05 5:24 
GeneralNot support UNICODEmemberchinkuanyeh10-Oct-04 21:28 
GeneralCR LF supportmemberlittle.mole3-Jul-04 10:29 
Questionmistake?member3m2u16-Mar-04 23:02 
Questionhow to converts a string that has been encoded for transmission in a URL into a decoded string?memberrafaelcn24-Mar-03 6:25 
AnswerRe: how to converts a string that has been encoded for transmission in a URL into a decoded string?memberlittle.mole3-Jul-04 11:23 
QuestionUnicode?memberAlexMarbus6-Nov-02 3:11 
Generalnot portablememberpamela7-Oct-01 2:42 
GeneralRe: not portablesussAnonymous17-May-03 9:15 
QuestionHow to add a dialog before Windows's explore workingmemberNewLearnXZX12-Jul-01 14:48 
GeneralWindows already does (most) of this, methinks...memberArnt Witteveen4-Jul-01 0:57 
GeneralRe: Windows already does (most) of this, methinks...memberRyszard Krakowiak9-Jul-01 4:35 
GeneralRe: Windows already does (most) of this, methinks...memberwangjj23-Jul-01 15:29 
GeneralRe: Windows already does (most) of this, methinks...sussAnonymous29-Aug-02 1:29 
GeneralMinor correction in UrlEncode1memberMarc Brooks25-Jun-01 18:14 
GeneralGetBuffer instead of newmemberAnonymous25-Jun-01 5:08 
GeneralGetBuffer instead of newmemberAnonymous25-Jun-01 5:08 
GeneralRe: GetBuffer instead of newmemberWictor Wilén1-Aug-01 10:59 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 26 Jun 2001
Article Copyright 2001 by Ryszard Krakowiak
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid