#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
tstring urlEncode(const TCHAR* szEncode)
{
tstring strEncoded;
static const TCHAR szUnsafe[] = {_T("/"<>%//^[]`+$,@:;/!#?=&")};
static const TCHAR szHexChr[] = {_T("0123456789ABCDEF")};
int nLength = lstrlen(szEncode);
for(int i = 0; i < nLength; i++)
{
TCHAR ch = szEncode[i];
if((NULL == _tcschr(szUnsafe, ch))
&& ((int)ch > 32)
&& ((int)ch < 123))
{
strEncoded += ch;
}
else
{
strEncoded += tstring(_T("%"));
strEncoded += szHexChr[(int)((ch >> 4) & 0x0f)];
strEncoded += szHexChr[(int)(ch & 0x0f)];
}
}
return strEncoded;
}