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. reading and writing a utf16 string inside a qdatastream operator
Forum Updated to NodeBB v4.3 + New Features

reading and writing a utf16 string inside a qdatastream operator

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 943 Views 1 Watching
  • 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.
  • StyxS Offline
    StyxS Offline
    Styx
    wrote on last edited by
    #3

    I dont understand? example?

    Christian EhrlicherC 1 Reply Last reply
    0
    • StyxS Styx

      I dont understand? example?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4
      struct People
      {
          quint32 Id;
          QString Name;
          quint32 Age;
      };
      
      inline QDataStream &operator<<(QDataStream &readstream, const People &m_people)
      {
          readstream << m_people.Id << m_people.Name << m_people.Age;
          return readstream;
      }
      
      inline QDataStream &operator>>(QDataStream &writestream, People &m_people)
      {
          writestream >> m_people.Id >> m_people.Name >> m_people.Age;
          return writestream;
      }
      
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • StyxS Offline
        StyxS Offline
        Styx
        wrote on last edited by
        #5

        I dont see how this can work?

        Lets say the datastream looks like this

        01 00 00 00 - ID
        05 00 - name length 
        64 75 6D 70 6C - name
        34 00 00 00 - age
        

        still doesn't populate the string. Only if i read the rawdata from the buffer.

        KroMignonK 1 Reply Last reply
        0
        • StyxS Styx

          I dont see how this can work?

          Lets say the datastream looks like this

          01 00 00 00 - ID
          05 00 - name length 
          64 75 6D 70 6C - name
          34 00 00 00 - age
          

          still doesn't populate the string. Only if i read the rawdata from the buffer.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #6

          @Styx said in reading and writing a utf16 string inside a qdatastream operator:

          still doesn't populate the string. Only if i read the rawdata from the buffer.

          First question what are you want to do:

          • read/write a QString from/to a file?
          • read/write a UTF-16 string from/to a file and store it in a QString?

          If it is the first, then QDataStream is able to serialize and deserialize a QString, there is nothing else to do. There is also no need of an extra variable to store string length. QString itself knows how long the string is!

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          1
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #7

            Where do you have the bytes from? You must only read and write with QDataStream.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • StyxS Offline
              StyxS Offline
              Styx
              wrote on last edited by
              #8

              Reading them from a Binary File. All strings are utf-16 before the string is the string length of the string.

              mrjjM 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @Styx said in reading and writing a utf16 string inside a qdatastream operator:

                Reading them from a Binary File.

                You can't use QDataStream for this. You have to write your own parser.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • StyxS Styx

                  Reading them from a Binary File. All strings are utf-16 before the string is the string length of the string.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @Styx
                  Hi
                  So this binary file is NOT one you made saving the People struct?
                  I mean you did not save this file yourself, but wants to read it in ?

                  1 Reply Last reply
                  0
                  • StyxS Offline
                    StyxS Offline
                    Styx
                    wrote on last edited by Styx
                    #11

                    I didnt use qdatastream to write it or read it.

                    My program works using writeRawData and readRawData

                    Just wondering if it was possible to use operators to read it much like the documents say.

                    Long as these binarys have no strings they can be read and written like @Christian-Ehrlicher posted...

                    const QVector<People> &getPeople() const; // grabs all the data in the vector to print it to a file.
                    

                    Only other question i had.. since my get method is const i cant use a binary operator

                    inline QDataStream &operator<<(QDataStream &readstream, const People &m_people)
                    {
                        readstream << m_people.Id << m_people.Name_Legth;
                    
                        if(m_people.Name_Legth <= 0)
                        {
                            m_people.Name = "";
                    	readstream << m_people.Name;
                        }
                        else
                        {
                            QByteArray Buffer( m_people.Name_Legth * 2, 0);
                            readstream.readRawData( Buffer.data(), m_people.Name_Legth * 2);
                            m_people.Name = QString::fromUtf16(reinterpret_cast<const ushort*>( Buffer.constData()));
                    	readstream << m_people.Name;
                        }
                        readstream << m_people.Age;
                        return readstream;
                    }
                    

                    is something like this even possible?

                    mrjjM 1 Reply Last reply
                    0
                    • StyxS Styx

                      I didnt use qdatastream to write it or read it.

                      My program works using writeRawData and readRawData

                      Just wondering if it was possible to use operators to read it much like the documents say.

                      Long as these binarys have no strings they can be read and written like @Christian-Ehrlicher posted...

                      const QVector<People> &getPeople() const; // grabs all the data in the vector to print it to a file.
                      

                      Only other question i had.. since my get method is const i cant use a binary operator

                      inline QDataStream &operator<<(QDataStream &readstream, const People &m_people)
                      {
                          readstream << m_people.Id << m_people.Name_Legth;
                      
                          if(m_people.Name_Legth <= 0)
                          {
                              m_people.Name = "";
                      	readstream << m_people.Name;
                          }
                          else
                          {
                              QByteArray Buffer( m_people.Name_Legth * 2, 0);
                              readstream.readRawData( Buffer.data(), m_people.Name_Legth * 2);
                              m_people.Name = QString::fromUtf16(reinterpret_cast<const ushort*>( Buffer.constData()));
                      	readstream << m_people.Name;
                          }
                          readstream << m_people.Age;
                          return readstream;
                      }
                      

                      is something like this even possible?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #12

                      @Styx

                      Yes its possible but you cannot do it via
                      readstream << m_people.Name;

                      as here you read in QString via QDataStream and Qstring would normally add its length
                      to the stream before the actual text data.

                      But since you did NOT use QDataStream to save the QString, it will read it wrong.

                      So you must read back your string using "raw" reading just as you wrote it.

                      Its important to understand that all Qt Types can save and load themself. They also save the size for
                      strings and containers.

                      So if you try to mix it with raw format, you have to be really careful as else it simply will read it wrong.

                      As @Christian-Ehrlicher shows, is how Qt does it by itself. No handling of sizes as the classes does that themself.

                      So you really are complicating it for your self and I hope there is a good reason as else
                      just changing to use QDataStream &operator<< and >> for saving and loading is much
                      less error-prone. :)

                      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