C++ 实现 urlencode,可以使用 php 的 url_decode() 解码。
注意 char * 与 String 的区别在 “\0”
inline BYTE toHex(const BYTE &x)
{
return x > 9 ? x + 55: x + 48;
}
CString CMyTools::urlEncoding( unsigned char * sIn, ULONGLONG len )
{
CString sOut;
for( ULONGLONG ix = 0; ix < len; ix++ )
{
BYTE buf[4];
memset( buf, 0, 4 );
if( isalnum( (BYTE)sIn[ix] ) )
{
buf[0] = sIn[ix];
}
else if ( isspace( (BYTE)sIn[ix] ) )
{
buf[0] = '+';
}
else
{
buf[0] = '%';
buf[1] = toHex( (BYTE)sIn[ix] >> 4 );
buf[2] = toHex( (BYTE)sIn[ix] % 16);
}
sOut += (char *)buf;
}
return sOut;
}
CString CMyTools::urlEncoding( CString sIn )
{
CString sOut;
for( int ix = 0; ix < sIn.GetLength(); ix++ )
{
BYTE buf[4];
memset( buf, 0, 4 );
if( isalnum( (BYTE)sIn[ix] ) )
{
buf[0] = sIn[ix];
}
else if ( isspace( (BYTE)sIn[ix] ) )
{
buf[0] = '+';
}
else
{
buf[0] = '%';
buf[1] = toHex( (BYTE)sIn[ix] >> 4 );
buf[2] = toHex( (BYTE)sIn[ix] % 16);
}
sOut += (char *)buf;
}
return sOut;
}