Connect QString with byte code
-
wrote on 12 Jan 2023, 18:24 last edited by
Hi,
I try send some text to RS232 device. But some letter are coded according withWPC1250
table code. So I try send text:QString sAddresHeader = "wroc\xB3aw" ;
but it doesnt work. Work when I send "wroc" 0xB3 "aw". It possible connect this in one string ?
-
Lifetime Qt Championwrote on 12 Jan 2023, 19:05 last edited by Chris Kawa 1 Dec 2023, 19:06
\x
escape sequence is greedy, meaning that it will take every following character that is a hex number, so if you write"wroc\xB3aw"
it will treat\xB3a
as a single character. To avoid this you need to break the string after the escape character. You can do it with a little trick like this:"wroc\xB3""aw"
.But that's just first of your multiple problems here.
"wroc\xB3""aw" is a character array encoded as WPC1250
QString is an UTF-16 container
QString operator= does a conversion assuming the source is UTF-8.
So this lineQString sAddresHeader = "wroc\xB3""aw" ;
takes a WPC1250 string, treats it as UTF-8 and converts that to UTF-16, which is of course nonsense.
If you want to store that string in QString you need to tell Qt what source encoding it is. For example:
QString sAddresHeader = QTextCodec::codecForName("Windows-1250")->toUnicode("wroc\xB3""aw");
this will do conversion from WPC1250 to UTF-16 and store it in QString.
But that's still not the end of it. As Christian mentioned you usually send bytes, not QStrings. It's important to know what encoding does the receiver expect - UTF or WPC1250. If you send the bytes of QString and receiver expects WPC1250 then you need to convert it back, either before you send it or after you receive it.
If the receiver expects WPC1250 then it's easier not to use QString as the storage, and simply store the WPC1250 bytes in QByteArray (again, like Christian suggested), soQByteArray sAddresHeader = "wroc\xB3""aw" ;
Btw. Greetings from sunny Wrocław :)
-
Hi,
I try send some text to RS232 device. But some letter are coded according withWPC1250
table code. So I try send text:QString sAddresHeader = "wroc\xB3aw" ;
but it doesnt work. Work when I send "wroc" 0xB3 "aw". It possible connect this in one string ?
@Damian7546 said in Connect QString with byte code:
but it doesnt work
What does this mean? How do you try to send the QString.
From my pov you won't send a string but a QByteArray... -
Lifetime Qt Championwrote on 12 Jan 2023, 19:05 last edited by Chris Kawa 1 Dec 2023, 19:06
\x
escape sequence is greedy, meaning that it will take every following character that is a hex number, so if you write"wroc\xB3aw"
it will treat\xB3a
as a single character. To avoid this you need to break the string after the escape character. You can do it with a little trick like this:"wroc\xB3""aw"
.But that's just first of your multiple problems here.
"wroc\xB3""aw" is a character array encoded as WPC1250
QString is an UTF-16 container
QString operator= does a conversion assuming the source is UTF-8.
So this lineQString sAddresHeader = "wroc\xB3""aw" ;
takes a WPC1250 string, treats it as UTF-8 and converts that to UTF-16, which is of course nonsense.
If you want to store that string in QString you need to tell Qt what source encoding it is. For example:
QString sAddresHeader = QTextCodec::codecForName("Windows-1250")->toUnicode("wroc\xB3""aw");
this will do conversion from WPC1250 to UTF-16 and store it in QString.
But that's still not the end of it. As Christian mentioned you usually send bytes, not QStrings. It's important to know what encoding does the receiver expect - UTF or WPC1250. If you send the bytes of QString and receiver expects WPC1250 then you need to convert it back, either before you send it or after you receive it.
If the receiver expects WPC1250 then it's easier not to use QString as the storage, and simply store the WPC1250 bytes in QByteArray (again, like Christian suggested), soQByteArray sAddresHeader = "wroc\xB3""aw" ;
Btw. Greetings from sunny Wrocław :)
-
\x
escape sequence is greedy, meaning that it will take every following character that is a hex number, so if you write"wroc\xB3aw"
it will treat\xB3a
as a single character. To avoid this you need to break the string after the escape character. You can do it with a little trick like this:"wroc\xB3""aw"
.But that's just first of your multiple problems here.
"wroc\xB3""aw" is a character array encoded as WPC1250
QString is an UTF-16 container
QString operator= does a conversion assuming the source is UTF-8.
So this lineQString sAddresHeader = "wroc\xB3""aw" ;
takes a WPC1250 string, treats it as UTF-8 and converts that to UTF-16, which is of course nonsense.
If you want to store that string in QString you need to tell Qt what source encoding it is. For example:
QString sAddresHeader = QTextCodec::codecForName("Windows-1250")->toUnicode("wroc\xB3""aw");
this will do conversion from WPC1250 to UTF-16 and store it in QString.
But that's still not the end of it. As Christian mentioned you usually send bytes, not QStrings. It's important to know what encoding does the receiver expect - UTF or WPC1250. If you send the bytes of QString and receiver expects WPC1250 then you need to convert it back, either before you send it or after you receive it.
If the receiver expects WPC1250 then it's easier not to use QString as the storage, and simply store the WPC1250 bytes in QByteArray (again, like Christian suggested), soQByteArray sAddresHeader = "wroc\xB3""aw" ;
Btw. Greetings from sunny Wrocław :)
wrote on 13 Jan 2023, 18:01 last edited by@Chris-Kawa said in Connect QString with byte code:
QByteArray sAddresHeader = "wroc\xB3""aw" ;
This is what I need .
QByteArray sAddresHeader = "Greetings from Rzesz\xF3""w" -
@Chris-Kawa said in Connect QString with byte code:
QByteArray sAddresHeader = "wroc\xB3""aw" ;
This is what I need .
QByteArray sAddresHeader = "Greetings from Rzesz\xF3""w"@Damian7546 That string breaking trick is needed only if a hex character follows the escape sequence, so any of A-F letters.
w
is not one of them, so in case of Rzeszów you can just do"Rzesz\xF3w"
-
wrote on 13 Jan 2023, 19:34 last edited by
Thank you for that string breaking trick Chris!
It has got me stumped a few times, in HTML it's easy just do a semicolon (Rzeszów
) but not so in C++ :-)
1/6