Url解码代码

Url解码代码

Url编码与解码的代码,使用WTL的CString,支持日文字符

 

CStringW ConvertUTF8ToUTF16(const CStringA& utf8)
{
    int wLen = MultiByteToWideChar(CP_UTF8, 0, utf8, utf8.GetLength(), 0, 0);

    CStringW buf;
    WCHAR*  dd = buf.GetBuffer(wLen);

    wLen = MultiByteToWideChar(CP_UTF8, 0, utf8, utf8.GetLength(), dd, wLen);

    buf.ReleaseBuffer(wLen);

    return buf;
}

CStringW DecodeUrl(const std::string& src)
{
    CStringW dstUrlW(L"");
    int srclen = (int)src.size();
    std::string dst;
    for (int i = 0; i < srclen; i++) {
        if (src[i] == '%') {
            if (isxdigit(src[i + 1]) && isxdigit(src[i + 2])) {
                char c1 = src[++i];
                char c2 = src[++i];
                c1 = c1 - 48 - ((c1 >= 'A') ? 7 : 0) - ((c1 >= 'a') ? 32 : 0);
                c2 = c2 - 48 - ((c2 >= 'A') ? 7 : 0) - ((c2 >= 'a') ? 32 : 0);
                dst += (unsigned char)(c1 * 16 + c2);
            }
        }
        else if (src[i] == '+') {
            dst += ' ';
        }
        else {
            dst += src[i];
        }
    }

    if (dst.size() > 0) {
        dstUrlW = ConvertUTF8ToUTF16(CStringA(dst.c_str()));
    }

    return dstUrlW;
}

void EncodeUrl(char* dst, const char* src)
{
    while(*src){
        unsigned char c = static_cast<unsigned char>(*src);
        if(c == ' ' ){
            *dst++ = '+';
        }
        else if( isalnum( c ) || c == '$' || c == '-' || c == '_' || c == '.' ||
            c == '!' || c == '*' || c == '/'' || c == '(' || c == ')' || c == ',' ){
            *dst++ = c;
        }
        else{
            *dst++ = '%';
            unsigned char nibble = (c >> 4) & 0xF;
            *dst++ = ( nibble < 10 ? nibble + '0' : nibble - 10 + 'A' );
            nibble = c & 0xF;
            *dst++ = ( nibble < 10 ? nibble + '0' : nibble - 10 + 'A' );
        }
        src++;
    }
    *dst = '/0';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值