7

I have the following example:

%3ca href%3d%22http://google.com%22%3eGoogle%3c/a%3e

When unescaped I expect this to be:

<a href="http://google.com">Google</a>

I've tried:

strUnescaped = QString::fromUtf8(strEncoded.toLatin1().data());

But the result is the same as the original unaffected and unmodified. What do I need to do?

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • @Jarod42, thank you, it didn't work, I tried: QUrl::fromEncoded(strEncoded.toLatin1()).toString(); and the result was an empty string. – SPlatten Oct 30 '18 at 08:49
  • `QUrl::fromEncoded` will decode [URLs](http://doc.qt.io/qt-5/qurl.html#ParsingMode-enum) not escaped HTML. There are functions to escape but I don't see functions to unescape : https://stackoverflow.com/a/14173350/6165833 – ymoreau Oct 30 '18 at 08:54
  • @ymoreau, thank you, bit unusual and a bit of a gap in the development. – SPlatten Oct 30 '18 at 08:54
  • Possible duplicate of [How to unescape XML in Qt?](https://stackoverflow.com/questions/14513992/how-to-unescape-xml-in-qt) – ymoreau Oct 30 '18 at 08:55
  • @ymoreau, I saw that, however I was hoping there was a built in solution as part of the Qt library. – SPlatten Oct 30 '18 at 08:56
  • This might be better solution : https://stackoverflow.com/questions/7696159/how-can-i-convert-entity-characterescape-character-to-html-in-qt – ymoreau Oct 30 '18 at 08:57
  • @ymoreau, at the same time I found: https://deepugeorge2007.wordpress.com/2014/06/10/decode-html-entities-in-qt/ I am trying this now. – SPlatten Oct 30 '18 at 08:59
  • @ymoreau, didn't work, result is the same. – SPlatten Oct 30 '18 at 09:04

1 Answers1

8

You might use QUrl::fromPercentEncoding to decode percent to regular character:

QString encodedStr = "%3ca href='http://google.com'%3eGoogle%3c/a%3e";
auto decodedStr = QUrl::fromPercentEncoding(encodedStr.toLatin1());
// decodedStr == "<a href='http://google.com'>Google</a>"
Jarod42
  • 203,559
  • 14
  • 181
  • 302