Click here to Skip to main content
14,243,297 members

URLEncode

Rate this:
3.50 (6 votes)
Please Sign up or sign in to vote.
3.50 (6 votes)
25 Jun 2001
Convert string using URLEncode method

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

Share

About the Author

Ryszard Krakowiak
Web Developer
Poland Poland
No Biography provided

Comments and Discussions

 
QuestionThis is slightly broke, and to clear up some confussion Pin
KevinSW21-Sep-12 1:35
memberKevinSW21-Sep-12 1:35 
GeneralHere's a much simpler method that supports Unicode Pin
dc_200025-Apr-11 0:59
memberdc_200025-Apr-11 0:59 
Questionwtf is "if (!isalnum(*pInTmp) && !isalnum(*pInTmp))" ? Pin
s98769026-Jul-07 23:34
members98769026-Jul-07 23:34 
GeneralURLEncode2() Unicode version Pin
mcanti2-May-06 2:28
membermcanti2-May-06 2:28 
GeneralUnicode solution Pin
angelo moscati4-Jul-05 3:41
memberangelo moscati4-Jul-05 3:41 
GeneralRe: Unicode solution Pin
angelo moscati4-Jul-05 5:24
memberangelo moscati4-Jul-05 5:24 
GeneralNot support UNICODE Pin
chinkuanyeh10-Oct-04 21:28
memberchinkuanyeh10-Oct-04 21:28 
GeneralCR LF support Pin
little.mole3-Jul-04 10:29
memberlittle.mole3-Jul-04 10:29 
Questionmistake? Pin
3m2u16-Mar-04 23:02
member3m2u16-Mar-04 23:02 
Questionhow to converts a string that has been encoded for transmission in a URL into a decoded string? Pin
rafaelcn24-Mar-03 6:25
memberrafaelcn24-Mar-03 6:25 
AnswerRe: how to converts a string that has been encoded for transmission in a URL into a decoded string? Pin
little.mole3-Jul-04 11:23
memberlittle.mole3-Jul-04 11:23 
QuestionUnicode? Pin
AlexMarbus6-Nov-02 3:11
memberAlexMarbus6-Nov-02 3:11 
Generalnot portable Pin
pamela7-Oct-01 2:42
memberpamela7-Oct-01 2:42 
GeneralRe: not portable Pin
Anonymous17-May-03 9:15
memberAnonymous17-May-03 9:15 
QuestionHow to add a dialog before Windows's explore working Pin
12-Jul-01 14:48
suss12-Jul-01 14:48 
GeneralWindows already does (most) of this, methinks... Pin
Arnt Witteveen4-Jul-01 0:57
memberArnt Witteveen4-Jul-01 0:57 
GeneralRe: Windows already does (most) of this, methinks... Pin
Ryszard Krakowiak9-Jul-01 4:35
memberRyszard Krakowiak9-Jul-01 4:35 
GeneralRe: Windows already does (most) of this, methinks... Pin
23-Jul-01 15:29
suss23-Jul-01 15:29 
GeneralRe: Windows already does (most) of this, methinks... Pin
Anonymous29-Aug-02 1:29
memberAnonymous29-Aug-02 1:29 
GeneralMinor correction in UrlEncode1 Pin
Marc Brooks25-Jun-01 18:14
memberMarc Brooks25-Jun-01 18:14 
GeneralGetBuffer instead of new Pin
25-Jun-01 5:08
suss25-Jun-01 5:08 
GeneralGetBuffer instead of new Pin
25-Jun-01 5:08
suss25-Jun-01 5:08 
GeneralRe: GetBuffer instead of new Pin
Wictor Wilén1-Aug-01 10:59
memberWictor Wilén1-Aug-01 10:59 

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.

Article
Posted 20 Jun 2001

Stats

160.9K views
1.3K downloads
21 bookmarked