PDA

View Full Version : convert QString to QByteArray



morgana
28th July 2008, 08:52
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))



QString memo_text=textEdit.toPlainText();
qDebug()<<memo_text; //Korean String value correct;
QByteArray textTemp(memo_text.toUtf8() ,1000);
qDebug()<<memo_text; //Korean String value incorrect;
strcpy(memo_info->text , textTemp.data());
qDebug()<<memo_text; //Korean String value incorrect;

aamer4yu
28th July 2008, 10:22
What if u try this -


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 -

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 :)

morgana
28th July 2008, 14:53
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?

lvi
28th July 2008, 15:08
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...

aamer4yu
28th July 2008, 18:51
If u want to get char* from QString, have a look at QString::toLocal8Bit

Also read the QString documentation completely.

masterbraind
2nd March 2011, 13:33
add .toAscii() after the Qbyte array in qDebug() line.