Click here to Skip to main content
6,596,602 members and growing! (20,817 online)
Email Password   helpLost your password?
General Programming » String handling » Text Conversion     Beginner

URLEncode

By Ryszard Krakowiak

Convert string using URLEncode method
VC6Win2K, MFC, Dev
Posted:20 Jun 2001
Updated:25 Jun 2001
Views:100,265
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 3.90 Rating: 3.61 out of 5
1 vote, 16.7%
1

2
1 vote, 16.7%
3

4
4 votes, 66.7%
5

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


Member

Occupation: Web Developer
Location: Poland Poland

Other popular String handling articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
Generalwtf is "if (!isalnum(*pInTmp) && !isalnum(*pInTmp))" ? Pinmembers9876900:34 27 Jul '07  
GeneralURLEncode2() Unicode version Pinmembermcanti3:28 2 May '06  
GeneralUnicode solution Pinmemberangelo moscati4:41 4 Jul '05  
GeneralRe: Unicode solution Pinmemberangelo moscati6:24 4 Jul '05  
GeneralNot support UNICODE Pinmemberchinkuanyeh22:28 10 Oct '04  
GeneralCR LF support Pinmemberlittle.mole11:29 3 Jul '04  
Generalmistake? Pinmember3m2u0:02 17 Mar '04  
Generalhow to converts a string that has been encoded for transmission in a URL into a decoded string? Pinmemberrafaelcn7:25 24 Mar '03  
GeneralRe: how to converts a string that has been encoded for transmission in a URL into a decoded string? Pinmemberlittle.mole12:23 3 Jul '04  
GeneralUnicode? PinmemberAlexMarbus4:11 6 Nov '02  
Generalnot portable Pinmemberpamela3:42 7 Oct '01  
GeneralRe: not portable PinsussAnonymous10:15 17 May '03  
GeneralHow to add a dialog before Windows's explore working PinmemberNewLearnXZX15:48 12 Jul '01  
GeneralWindows already does (most) of this, methinks... PinmemberArnt Witteveen1:57 4 Jul '01  
GeneralRe: Windows already does (most) of this, methinks... PinmemberRyszard Krakowiak5:35 9 Jul '01  
GeneralRe: Windows already does (most) of this, methinks... Pinmemberwangjj16:29 23 Jul '01  
GeneralRe: Windows already does (most) of this, methinks... PinsussAnonymous2:29 29 Aug '02  
GeneralMinor correction in UrlEncode1 PinmemberMarc Brooks19:14 25 Jun '01  
GeneralGetBuffer instead of new PinmemberAnonymous6:08 25 Jun '01  
GeneralGetBuffer instead of new PinmemberAnonymous6:08 25 Jun '01  
GeneralRe: GetBuffer instead of new PinmemberWictor Wilén11:59 1 Aug '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jun 2001
Editor: James Spibey
Copyright 2001 by Ryszard Krakowiak
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project