Skip to main content
Email Password   helpLost your password?

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)

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalwtf is "if (!isalnum(*pInTmp) && !isalnum(*pInTmp))" ? Pin
s987690
0:34 27 Jul '07  
GeneralURLEncode2() Unicode version Pin
mcanti
3:28 2 May '06  
GeneralUnicode solution Pin
angelo moscati
4:41 4 Jul '05  
GeneralRe: Unicode solution Pin
angelo moscati
6:24 4 Jul '05  
GeneralNot support UNICODE Pin
chinkuanyeh
22:28 10 Oct '04  
GeneralCR LF support Pin
little.mole
11:29 3 Jul '04  
Generalmistake? Pin
3m2u
0:02 17 Mar '04  
Generalhow to converts a string that has been encoded for transmission in a URL into a decoded string? Pin
rafaelcn
7:25 24 Mar '03  
GeneralRe: how to converts a string that has been encoded for transmission in a URL into a decoded string? Pin
little.mole
12:23 3 Jul '04  
GeneralUnicode? Pin
AlexMarbus
4:11 6 Nov '02  
Generalnot portable Pin
pamela
3:42 7 Oct '01  
GeneralRe: not portable Pin
Anonymous
10:15 17 May '03  
GeneralHow to add a dialog before Windows's explore working Pin
NewLearnXZX
15:48 12 Jul '01  
GeneralWindows already does (most) of this, methinks... Pin
Arnt Witteveen
1:57 4 Jul '01  
GeneralRe: Windows already does (most) of this, methinks... Pin
Ryszard Krakowiak
5:35 9 Jul '01  
GeneralRe: Windows already does (most) of this, methinks... Pin
wangjj
16:29 23 Jul '01  
GeneralRe: Windows already does (most) of this, methinks... Pin
Anonymous
2:29 29 Aug '02  
GeneralMinor correction in UrlEncode1 Pin
Marc Brooks
19:14 25 Jun '01  
GeneralGetBuffer instead of new Pin
Anonymous
6:08 25 Jun '01  
GeneralGetBuffer instead of new Pin
Anonymous
6:08 25 Jun '01  
GeneralRe: GetBuffer instead of new Pin
Wictor Wilén
11:59 1 Aug '01  


Last Updated 25 Jun 2001 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009