
Originally Posted by
wysota
I'd prefer something like that (not tested):
int pos = 0;
while((pos=rx.indexIn(stringToDecode, pos))!=-1){
uint val = rx.cap(1).toUInt(0, 16);
}
QString stringToDecode;
QRegExp rx("%(\\d\\d)");
int pos = 0;
while((pos=rx.indexIn(stringToDecode, pos))!=-1){
uint val = rx.cap(1).toUInt(0, 16);
stringToDecode.replace(pos, 3, QString(QChar(val)));
}
To copy to clipboard, switch view to plain text mode
After this "stringToDecode" should contain the converted string.
I recommend this code - it eliminates errors caused by previous code (hex A-F are not decimal = unrecognized by previous code, "hi%25there" represents "hi%there" = causes unwanted character creation "%th" - last replaced character has to be jumped over)
int pos = 0;
while ((pos = rx.indexIn(stringToDecode, pos)) != -1) {
stringToDecode.
replace(pos,
3,
QString(QChar(rx.
cap(1).
toUInt(0,
16))));
pos++; // jumps over currently replaced character
}
QString stringToDecode;
QRegExp rx("%(\\S\\S)");
int pos = 0;
while ((pos = rx.indexIn(stringToDecode, pos)) != -1) {
stringToDecode.replace(pos, 3, QString(QChar(rx.cap(1).toUInt(0, 16))));
pos++; // jumps over currently replaced character
}
To copy to clipboard, switch view to plain text mode
Bookmarks