Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QUrl toPercentEncoding() lowercase hex

I have a string that becomes the query string part of a url and has to be url encoded.

QString queryStringParam = "J1mOEfg/TC";

So, I tried to use QUrl::toPercentEncoding() like this

QString encodedQueryString = QUrl::toPercentEncoding(queryStringParam);

which results in "J1mOEfg%2FT" but the webservice that I'm calling is expecting "J1mOEfg%2fT" [note the lowercase 'f' in the encoding of '/' to '%20f'] and hence rejects the parameter. It's probably bad on the service's side but I can't do anything about it. How can I get lowercase encoding hex chars while maintaining the case in the rest of the parameter?

like image 469
Professor Chaos Avatar asked Sep 27 '26 15:09

Professor Chaos


1 Answers

I don't think you can do it with standard functions, but you can easily write your own. In percent-encoded strings special characters are encoded with a % symbol that is followed by a pair of hex digits. Knowing that, you can write a function like this:

QString lowerPercentEncoding( QString str )
{
    int index = str.indexOf("%");
    while( index != -1 )
    {
        QString tmp = str.mid(index, 3);
        str.replace(index,3,tmp.toLower());
        index = str.indexOf("%", index + 1);
    }
    return str;
}
like image 109
SingerOfTheFall Avatar answered Sep 29 '26 05:09

SingerOfTheFall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!