Results 1 to 6 of 6

Thread: convert QString to QByteArray

  1. #1
    Join Date
    Jul 2008
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Qt Code:
    1. QString memo_text=textEdit.toPlainText();
    2. qDebug()<<memo_text; //Korean String value correct;
    3. QByteArray textTemp(memo_text.toUtf8() ,1000);
    4. qDebug()<<memo_text; //Korean String value incorrect;
    5. strcpy(memo_info->text , textTemp.data());
    6. qDebug()<<memo_text; //Korean String value incorrect;
    To copy to clipboard, switch view to plain text mode 
    Last edited by morgana; 28th July 2008 at 09:57.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert QString to QByteArray

    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

  3. #3
    Join Date
    Jul 2008
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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?
    Last edited by morgana; 28th July 2008 at 17:13.

  4. #4
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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...

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert QString to QByteArray

    If u want to get char* from QString, have a look at QString::toLocal8Bit
    Also read the QStringdocumentation completely.

  6. #6
    Join Date
    May 2009
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: convert QString to QByteArray

    add .toAscii() after the Qbyte array in qDebug() line.

Similar Threads

  1. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 14th June 2008, 00:05
  2. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 10:10
  3. QByteArray to QString
    By babu198649 in forum Newbie
    Replies: 7
    Last Post: 6th December 2007, 14:08
  4. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 09:07
  5. How to convert from QString to quint16 ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 31st March 2006, 10:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.