convert QString to QByteArray
Hello Qt users.
I got trouble in convert QString to QByteArray.
I got Korean PlainText from textEdit.
it stored in QString.
So when i print QString the value is correct value.
But QByteArray value from The QString value is incorrect.
What i missed?(OS: Kubuntu(utf8))
Code:
QString memo_text
=textEdit.
toPlainText();
qDebug()<<memo_text; //Korean String value correct;
qDebug()<<memo_text; //Korean String value incorrect;
strcpy(memo_info->text , textTemp.data());
qDebug()<<memo_text; //Korean String value incorrect;
Re: convert QString to QByteArray
What if u try this -
Quote:
QString memo_text=textEdit.toPlainText();
qDebug()<<memo_text;
QByteArray textTemp = memo_text.toUtf8() ;
qDebug()<<textTemp;
Dou you get proper values ?? I didnt understand why u were passing 1000.
If you want to extract the first 1000 bytes, u cud use -
Quote:
QByteArray textTemp = memo_text.toUtf8().left(1000) ;
The QByteArray::QByteArray ( const char * data, int size ) accepts char* pointer, while u were passing QByteArray to it. May be thats causing problem :)
Re: convert QString to QByteArray
hm...
I cant explain my trouble with situation exactly.(I cant speak english well)
1. get text(Korean text) from QTextEdit(plaintext), it stored in QString.
2. I'll send QString value to The Server.
(QString valude stored in char array[1000]and write to The Sever)
3. Sever is printf("%s",recv_data) in C Language.
QString(from QTextEdit) is already correct unicode(is it right?)
(qDebug<<string print correct Korean Text perfectly.)
so when i use .toUtf8() it will broken is it right?
I want convert QString(Korean text) to char[](c language unicode).
what can i do?
Re: convert QString to QByteArray
If I'm not mistaken, Unicode characters take 2 bytes. So if you have a QString in Unicode and convert it to a char-array, sprinft() will print something else than you want. If you were using C++, I'd say to use wchar_t...
Re: convert QString to QByteArray
If u want to get char* from QString, have a look at QString::toLocal8Bit
Also read the QStringdocumentation completely.
Re: convert QString to QByteArray
add .toAscii() after the Qbyte array in qDebug() line.