QString to ASCII QByteArray
-
Hello,
I need to convert aQString
data into an ASCII containingQByteArray
(I need to have the ^Z character in it). In Qt 4 there was theQString::toAscii
method however it was deprecated in favor ofQString::toLatin1
. There's note put in the documentation about said method, though:The returned byte array is undefined if the string contains non-Latin1 characters. Those characters may be suppressed or replaced with a question mark.
As far as I know, the latin1 charset (ISO/IEC 8859-1:1998) doesn't specify anything about the codes for non-printable characters under 0x20. So my question boils down to:
Can I useQString::toLatin1
, and will my^Z
character be preserved?Kind regards.
-
@kshegunov said:
I need to convert a QString data into an ASCII containing QByteArray (I need to have the ^Z character in it).
Just an idea: why not use QString::toUtf8? Since all UTF-8 characters less than 128 are the same as ASCII, then that should give you your ^Z character correctly.
If your string happens to contain characters greater than 127, then what would you expect to get in the QByteArray? Latin-1? UTF-8? Something else? Where is the QByteArray going / being used?
Depending on your answers, its possible that what you really want is one or more of:
- QString::toLocal8Bit
- QString::toUtf8
- not use QString at all, but start and end with QByteArray (if you're dealing with binary data).
In Qt 4 there was the QString::toAscii method however it was deprecated in favor of QString::toLatin1.
For what its worth, here's what source code for QString::toAscii looked like in Qt4:
QByteArray QString::toAscii() const { #ifndef QT_NO_TEXTCODEC if (codecForCStrings) return codecForCStrings->fromUnicode(*this); #endif // QT_NO_TEXTCODEC return toLatin1(); }
So you were probably getting either equivalent behaviour to QString::toLocal8Bit, or QString::toLatin1? No wonder the function was deprecated... its behaviour is pretty ambiguous, and host-configuration dependant (when you look into the default text codec).
Cheers.
-
@Paul-Colby
That was fast! ;)Just an idea: why not use QString::toUtf8? Since all UTF-8 characters less than 128 are the same as ASCII, then that should give you your ^Z character correctly.
Works for me, I don't know why I didn't think of that in the first place ... :D
If your string happens to contain characters greater than 127
It doesn't.
Something else? Where is the QByteArray going / being used?
It's going through the serial port, but I am dealing with text data. (
QSerialPort
expects aQByteArray
, and the device accepts ASCII data, possibly other charsets but I don't want to deal with them ...)No wonder the function was deprecated... its behaviour is pretty ambiguous, and host-configuration dependant
QString::toLocal8Bit
is host-configuration dependent as well, but as I said I'm dealing with latin text only + a few non-printables (under 0x20).Thanks for the suggestion(s)! I'll stick to utf8 and everything should be just fine.
Kind regards.