Results 1 to 4 of 4

Thread: QByteArray to QString

  1. #1
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    1

    Default QByteArray to QString

    I have a problem when converting QByteArray to QString and now I need a helper.

    I have a file.txt contain "Công nghệ thông tin" :: it is a Vietnamese string.
    I read file to byteArray (byteArray is a variable QByteArray type) .

    Then, I convert it to string (string is a variable QString type).
    code: QString string(byteArray);

    But, occuring a error when display text "Công nghệ thông tin".
    I think reason is encoding error.
    I want a QString Utf8.

    Please, help me, thanks. (sorry for my English level).

  2. #2
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Arrow Re: QByteArray to QString

    hi..
    you can use this function below to convert to string.

    public String Byte2Array(Byte[] b){
    String str="";
    for(int i=0;i<=b.length(),i++){
    str = str + (char)byte[i];
    }
    print(str);
    }

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QByteArray to QString

    Do you need that byte array? or is it just for reading the file?

    I suggest you to use QTextStream to read the file (with a proper encoding). Then you have the content in a QString which you can convert to a byte array if needed.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray to QString

    You should start your QByteArray with a unicode BOM, that way QString can detect the proper encoding correctly.

    ketan007: That function is built into QString.

Similar Threads

  1. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  2. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  3. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  4. QByteArray to QString
    By babu198649 in forum Newbie
    Replies: 7
    Last Post: 6th December 2007, 13:08
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.