Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Some Thing Wrong When I Save QString To File With Unicode Bom ?
Forum Update on Monday, May 27th 2025

Some Thing Wrong When I Save QString To File With Unicode Bom ?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 781 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • joeQJ Offline
    joeQJ Offline
    joeQ
    wrote on last edited by
    #1

    Hi, I want to save QString to file with the Unicode Bom, like the following Code

    #include <QCoreApplication>
    
    #include <QString>
    #include <QFile>
    #include <QTextStream>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QString str = QString::fromUtf8("你好,我是QString");
        QFile file;
        QTextStream out;
    
        file.setFileName("a.txt");
        if( !file.open( QIODevice::WriteOnly | QIODevice::Text) )
        {
            qDebug() << file.errorString();
            return 0;
        }
    
        out.setDevice(&file);
        out.setCodec("UTF-8");
        out.setGenerateByteOrderMark(true);
        out << str;
        file.close();
        qDebug() << "OK!";
    
        return a.exec();
    }
    

    But, when i use the vim :%!xxd to see the hex. I am not found the 0xffef.

    Maybe there is a better way to save QString to file with unicode and bom x0ffef ?

    Just do it!

    joeQJ 1 Reply Last reply
    0
    • joeQJ joeQ

      Hi, I want to save QString to file with the Unicode Bom, like the following Code

      #include <QCoreApplication>
      
      #include <QString>
      #include <QFile>
      #include <QTextStream>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          QString str = QString::fromUtf8("你好,我是QString");
          QFile file;
          QTextStream out;
      
          file.setFileName("a.txt");
          if( !file.open( QIODevice::WriteOnly | QIODevice::Text) )
          {
              qDebug() << file.errorString();
              return 0;
          }
      
          out.setDevice(&file);
          out.setCodec("UTF-8");
          out.setGenerateByteOrderMark(true);
          out << str;
          file.close();
          qDebug() << "OK!";
      
          return a.exec();
      }
      

      But, when i use the vim :%!xxd to see the hex. I am not found the 0xffef.

      Maybe there is a better way to save QString to file with unicode and bom x0ffef ?

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @joeQ

      oh, I know what i wrong! if i want to save file with x0ffef, i should use the 'utf-16'.

      #include <QCoreApplication>
      
      #include <QString>
      #include <QFile>
      #include <QTextStream>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          QString str = QString("你好,我是QString");
          QFile file;
          QTextStream out;
      
          file.setFileName("a.txt");
          if( !file.open( QIODevice::WriteOnly | QIODevice::Text) )
          {
              qDebug() << file.errorString();
              return 0;
          }
      
          out.setDevice(&file);
          out.setCodec("UTF-16"); ///< hould use the utf-16
          out.setGenerateByteOrderMark(true);
          out << str;
          file.close();
          qDebug() << "OK!";
      
          return a.exec();
      }
      

      Just do it!

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved