unsigned char toHex(const unsigned char &x)
{
return x > 9 ? x - 10 + 'A' : x + '0';
}
unsigned char fromHex(const unsigned char &x)
{
return isdigit(x) ? x - '0' : x - 'A' + 10;
}
string URLEncode(const char *sIn)
{
string sOut;
for (size_t ix = 0; ix < strlen(sIn); ix++)
{
unsigned char buf[4];
memset(buf, 0, 4);
if (isalnum((unsigned char)sIn[ix]))
{
buf[0] = sIn[ix];
}
else
{
buf[0] = '%';
buf[1] = toHex((unsigned char)sIn[ix] >> 4);
buf[2] = toHex((unsigned char)sIn[ix] % 16);
}
sOut += (char *)buf;
}
return sOut;
};
string URLDecode(const char *sIn)
{
string sOut;
for (size_t ix = 0; ix < strlen(sIn); ix++)
{
unsigned char ch = 0;
if (sIn[ix] == '%')
{
ch = (fromHex(sIn[ix + 1]) << 4);
ch |= fromHex(sIn[ix + 2]);
ix += 2;
}
else if (sIn[ix] == '+')
{
ch = ' ';
}
else
{
ch = sIn[ix];
}
sOut += (char)ch;
}
return sOut;
}
C++ URL编解码
最新推荐文章于 2023-03-02 17:00:52 发布