Results 1 to 4 of 4

Thread: UTF-16 files

  1. #1
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    16
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default UTF-16 files

    Hello,
    I am processing files that are usually "ASCII text, with CRLF line terminators" but, once in a while I am getting the data (only numbers and simple english characters) in "Little-endian UTF-16 Unicode character data, with CRLF line terminators" files. How can I recognize those UTF-16 files and convert them into ASCII text files?
    Many thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UTF-16 files

    ASCII text shouldn't contain 0x00 bytes, while in UTF-16 around 50% of bytes should be 0x00. You can also ask the user.
    Last edited by jacek; 6th September 2006 at 23:22. Reason: typo

  3. #3
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    16
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: UTF-16 files

    Hi,
    Well, I did it the way below and it works fine for my purpose and for the specific files I am dealing with but I am not sure how general it is:
    Qt Code:
    1. 38 QFile in(QString::fromStdString("datazip/temp/" + fileName));
    2. 39 in.open(QIODevice::ReadOnly);
    3. 40 QByteArray bufIn, bufOut;
    4. 41 bufIn = in.readAll();
    5. 42 in.close();
    6. 43 if((int)bufIn.at(0) < 0)
    7. 44 {
    8. 45 for(int i(0); i<bufIn.size(); ++i)
    9. 46 if((int) bufIn.at(i) > 0)
    10. 47 bufOut.append(bufIn.at(i));
    11. 48 QFile out(QString::fromStdString("datazip/temp/" + fileName));
    12. 49 out.open(QIODevice::WriteOnly);
    13. 50 out.write(bufOut);
    14. 51 out.close();
    15. 52 }
    To copy to clipboard, switch view to plain text mode 
    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UTF-16 files

    Quote Originally Posted by jcr
    Well, I did it the way below and it works fine for my purpose and for the specific files I am dealing with but I am not sure how general it is:
    Iff those files contain only ASCII characters, but are encoded in UTF-16, there shouldn't be any problem.

    Quote Originally Posted by jcr
    if((int)bufIn.at(0) > 0)
    Qt Code:
    1. if( bufIn.at(0) != 0 )
    To copy to clipboard, switch view to plain text mode 
    might be safer.

  5. The following user says thank you to jacek for this useful post:

    jcr (7th September 2006)

Similar Threads

  1. qt3 themes example: missing header files?
    By nikita in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2006, 12:12
  2. Qt4: QDir::entryList bug in windows when listing network files?
    By Yvon Halbwachs in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 14:22
  3. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28
  4. uic not creating seperate .h and .cpp files
    By Kapil in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2006, 10:43
  5. Visual Studio 2003 and .vcproj files
    By mbjerkne in forum General Discussion
    Replies: 2
    Last Post: 1st February 2006, 00:51

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.