4 ms·
Qt has QString::fromUtf8 which takes a QByteArray, and QString.toUtf8 which gives a QByteArray. (And similar for a few other encodings.) That was always obvious
by majewsky 10y ago
Qt has QString::fromUtf8 which takes a QByteArray, and QString.toUtf8 which gives a QByteArray. (And similar for a few other encodings.) That was always obvious enough for me.
http://doc.qt.io/qt-5/qstring.html http://doc.qt.io/qt-5/qstring.html
- icebraining 10y agoLike I asked baq, say I don't know the encoding ahead of time, but get it dynamically (like in the declaration of an XML file). How would I decode the string?
- protomikron 10y agoI would try utf-8, and otherwise just panic ("invalid input: string is not utf-8 encoded"). Obviously it depends on the context, but I think parsers should be strict, otherwise ambiguities leak into your system.
- icebraining 10y agoI'm not saying "try multiple encodings till you get something". I'm asking about the API when the encoding is not static, but provided at runtime, like in XML files, eg: <?xml version="1.0" encoding="ISO-8859-1"?>
- majewsky 10y agoYou would have the QByteArray first (e.g. read from a file or socket), parse the <?xml?>, thus find the encoding, convert the rest to QString with that, and parse the rest from the QString.
- icebraining 10y agoMy question is how does one "convert the rest to QString with that", in terms of actual code?