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. Writing regular text data to a QDataStream (or writing binary data to QTextStream)
Forum Updated to NodeBB v4.3 + New Features

Writing regular text data to a QDataStream (or writing binary data to QTextStream)

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 695 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.
  • swirlS Offline
    swirlS Offline
    swirl
    wrote on last edited by
    #1

    Title, for my code I've got a binary file format which has mostly binary data but also has null-terminated text in it for a name/description.

    So, I tried writing it with QTextStream, which resulted in some really weird results. I was trying to write a 12-byte long vector (specifically, a Botan::secure_vector<uint8_t>. I tried this by converting it to a QByteArray, but it wrote it as if it was in its "\xXX" form which didn't work of course. I tried looping through the vector, and writing each individual byte to it as a QChar, but this was even stranger. Using qDebug, the output of the IV itself was std::vector(164, 204, 3, 146, 159, 255, 163, 30, 59, 11, 162, 211), but when I read the file back, I get std::vector(194, 164, 195, 140, 3, 194, 146, 194, 159, 195, 191, 239, 191, 189). I have no idea what could possibly cause this.

    I also tried writing it as a const char *. In this case, before, it was std::vector(244, 204, 239, 205, 113, 104, 26, 15, 181, 0, 244, 219) and after reading it back, I got std::vector(239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189). Yet again, what's going on here? It's literally just a stream of 239, 191, 189. Sometimes it would put that sequence before every single byte.

    So yeah, after that, I tried QDataStream, which worked except that all the text was serialized as a QString, so 4 bytes of length then the contents. I didn't want this and just wanted to write the text itself (and of course, null-terminate it). And I couldn't get anything to work, I tried writing as const char * and QByteArray.

    Now that that's out of the way, what exactly should I do here?

    the

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

      When you don't want to use the format of QDataStream then use ofstream or something else. The format of QDataStream is fixed and can not be changed. The output of a QDataStream is also only to be used with QDataStream and nothing else. So either accept the 4 bytes or do it by your own / with another class.

      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
      4
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @swirl said in Writing regular text data to a QDataStream (or writing binary data to QTextStream):

        Now that that's out of the way, what exactly should I do here?

        If you want a custom format, you have to write it out by yourself. QDataStream needs those bytes to know the length of the string later on when reading the data.

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

        swirlS 1 Reply Last reply
        4
        • Christian EhrlicherC Christian Ehrlicher

          @swirl said in Writing regular text data to a QDataStream (or writing binary data to QTextStream):

          Now that that's out of the way, what exactly should I do here?

          If you want a custom format, you have to write it out by yourself. QDataStream needs those bytes to know the length of the string later on when reading the data.

          swirlS Offline
          swirlS Offline
          swirl
          wrote on last edited by
          #3

          @Christian-Ehrlicher wdym "write it out by myself"?

          this worked when I used an ofstream, and like I said it's a null-terminated string.

          the

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

            When you don't want to use the format of QDataStream then use ofstream or something else. The format of QDataStream is fixed and can not be changed. The output of a QDataStream is also only to be used with QDataStream and nothing else. So either accept the 4 bytes or do it by your own / with another class.

            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
            4

            • Login

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