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. Qt QString add null terminator
Qt 6.11 is out! See what's new in the release blog

Qt QString add null terminator

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 4.0k 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.
  • C Offline
    C Offline
    ConductedForce
    wrote on last edited by
    #1

    How do you add a null terminator at the end of a QString? I'm sending data across the network and the receiving device expects one.

    JKSHJ 1 Reply Last reply
    0
    • C ConductedForce

      How do you add a null terminator at the end of a QString? I'm sending data across the network and the receiving device expects one.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi, and welcome!

      @ConductedForce said in Qt QString add null terminator:

      How do you add a null terminator at the end of a QString? I'm sending data across the network and the receiving device expects one.

      In ASCII, the null-terminator is the null character, 0x00. It is represented as the '\0' literal in C/C++.

      Note: Technically, you send a byte array across the network; you don't send a text string.

      First, convert your QString to a QByteArray. Then, append a '\0' character to the end of the byte array. Then, send your byte array.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      JonBJ 1 Reply Last reply
      5
      • JKSHJ JKSH

        Hi, and welcome!

        @ConductedForce said in Qt QString add null terminator:

        How do you add a null terminator at the end of a QString? I'm sending data across the network and the receiving device expects one.

        In ASCII, the null-terminator is the null character, 0x00. It is represented as the '\0' literal in C/C++.

        Note: Technically, you send a byte array across the network; you don't send a text string.

        First, convert your QString to a QByteArray. Then, append a '\0' character to the end of the byte array. Then, send your byte array.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @JKSH
        Since this topic is marked as solved now anyway, I feel like observing:

        First, convert your QString to a QByteArray. Then, append a '\0' character to the end of the byte array. Then, send your byte array.

        Actually, according to me, you could skip appending that \0 character. When you have a QByteArray, QByteArray::data() is (surprisingly) always NUL-terminated, with the \0 being an additional character at the end (https://doc.qt.io/qt-5/qbytearray.html#data)! So, you can actually send QByteArray::size() + 1 bytes from data() and save on extending the byte array :)

        JKSHJ 1 Reply Last reply
        5
        • JonBJ JonB

          @JKSH
          Since this topic is marked as solved now anyway, I feel like observing:

          First, convert your QString to a QByteArray. Then, append a '\0' character to the end of the byte array. Then, send your byte array.

          Actually, according to me, you could skip appending that \0 character. When you have a QByteArray, QByteArray::data() is (surprisingly) always NUL-terminated, with the \0 being an additional character at the end (https://doc.qt.io/qt-5/qbytearray.html#data)! So, you can actually send QByteArray::size() + 1 bytes from data() and save on extending the byte array :)

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @JonB said in Qt QString add null terminator:

          you could skip appending that \0 character. When you have a QByteArray, QByteArray::data() is (surprisingly) always NUL-terminated, with the \0 being an additional character at the end (https://doc.qt.io/qt-5/qbytearray.html#data)! So, you can actually send QByteArray::size() + 1 bytes from data() and save on extending the byte array :)

          Good catch!

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0

          • Login

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