Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. C to C++, char * and QByteArray - General questions.
Forum Update on Monday, May 27th 2025

C to C++, char * and QByteArray - General questions.

Scheduled Pinned Locked Moved Solved The Lounge
11 Posts 5 Posters 1.3k 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.
  • artwawA Offline
    artwawA Offline
    artwaw
    wrote on last edited by
    #1

    Good morning.

    I have some spare time and I decided to spend it doing practical exercise: I have a bit of perfectly fine working C code and I'd like to write same code in Qt C++. It goes fairly well I'd say but my knowledge of the more low level C++ and C in general is rather limited. So I spend lot of the time reading documentation etc. and I consider it a good time.
    But there are some things that I am not certain of. Mainly, conversion between char * and QByteArray.

    I know I can get char * by simply using constData() and I need to keep an eye on the byte array going out of scope while doing it. But how is the size of said array "convertible"? QByteArray::size() returns the size of an array minus the trailing \0 if I am not mistaken, so it is a matter of +1 here and there... But if I am to call a method that accepts char * and also requires sizeOf() that char - do I pass QByteArray::size() or size()+1?

    And other way around. Let's say I need to call a method from external library, method(char * destination, char * src, size_t length), much like strlcat - what is the way of doing it using QByteArray if at all doable? (I leave a question of the sense of doing it on the side here)

    Lastly, since I already mentioned strlcat - QByteArray of doing it would be via QByteArray::append() - maybe a silly question but I found docs inconclusive: if I am to append char * (null terminated) to already null terminated QByteArray that way - will the trailing \0 be removed before append or left and I'll end up with two \0 in the array?

    Small disclaimer - I know that converting perfectly fine C to C++ rarely makes sense since there is no gain in speed (am I right here? I suppose I am) or anything except readability (I think) - it's just an exercise for me to broaden my knowledge.

    Thank you in advance for the time spent explaining those basics I miss.

    For more information please re-read.

    Kind Regards,
    Artur

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

      @artwaw said in C to C++, char * and QByteArray - General questions.:

      strlcat

      It's documented - so you have to look what your function you call needs.

      "This means that for strlcpy() src must be NUL-terminated and for strlcat() both src and dst must be NUL-terminated."

      "The strlcat() function appends the NUL-terminated string src to the end of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-terminating the result."

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

      artwawA 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @artwaw said in C to C++, char * and QByteArray - General questions.:

        strlcat

        It's documented - so you have to look what your function you call needs.

        "This means that for strlcpy() src must be NUL-terminated and for strlcat() both src and dst must be NUL-terminated."

        "The strlcat() function appends the NUL-terminated string src to the end of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-terminating the result."

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #3

        @Christian-Ehrlicher Yes, I have no problems with strlcat - my question was rather as to how QByteArray does append(). Sorry if that was not clear.

        For more information please re-read.

        Kind Regards,
        Artur

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

          It simply adds the character to the end - what should it do else? It's an array of bytes so appending a '\0' will append this too.

          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
          • artwawA artwaw

            Good morning.

            I have some spare time and I decided to spend it doing practical exercise: I have a bit of perfectly fine working C code and I'd like to write same code in Qt C++. It goes fairly well I'd say but my knowledge of the more low level C++ and C in general is rather limited. So I spend lot of the time reading documentation etc. and I consider it a good time.
            But there are some things that I am not certain of. Mainly, conversion between char * and QByteArray.

            I know I can get char * by simply using constData() and I need to keep an eye on the byte array going out of scope while doing it. But how is the size of said array "convertible"? QByteArray::size() returns the size of an array minus the trailing \0 if I am not mistaken, so it is a matter of +1 here and there... But if I am to call a method that accepts char * and also requires sizeOf() that char - do I pass QByteArray::size() or size()+1?

            And other way around. Let's say I need to call a method from external library, method(char * destination, char * src, size_t length), much like strlcat - what is the way of doing it using QByteArray if at all doable? (I leave a question of the sense of doing it on the side here)

            Lastly, since I already mentioned strlcat - QByteArray of doing it would be via QByteArray::append() - maybe a silly question but I found docs inconclusive: if I am to append char * (null terminated) to already null terminated QByteArray that way - will the trailing \0 be removed before append or left and I'll end up with two \0 in the array?

            Small disclaimer - I know that converting perfectly fine C to C++ rarely makes sense since there is no gain in speed (am I right here? I suppose I am) or anything except readability (I think) - it's just an exercise for me to broaden my knowledge.

            Thank you in advance for the time spent explaining those basics I miss.

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

            @artwaw said in C to C++, char * and QByteArray - General questions.:

            I found docs inconclusive: if I am to append char * (null terminated) to already null terminated QByteArray that way - will the trailing \0 be removed before append or left and I'll end up with two \0 in the array?

            You won't end up with two \0 in the array.

            Here is the example at https://doc.qt.io/qt-5/qbytearray.html#append extended:

            QByteArray x1("free");
            QByteArray y1("dom");
            x1.append(y);
            
            QByteArray x2("free");
            x2.append("dom"); // appending const char*
            

            Both x1 and x2 will contain "freedom" (or more precisely, "freedom\0")

            Conventionally in C, \0 is a marker that means "end of string". So, if you have a char array that contains "free\0dom\0" most C functions will see that as a 4-character string ("free").

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

            artwawA 1 Reply Last reply
            4
            • JKSHJ JKSH

              @artwaw said in C to C++, char * and QByteArray - General questions.:

              I found docs inconclusive: if I am to append char * (null terminated) to already null terminated QByteArray that way - will the trailing \0 be removed before append or left and I'll end up with two \0 in the array?

              You won't end up with two \0 in the array.

              Here is the example at https://doc.qt.io/qt-5/qbytearray.html#append extended:

              QByteArray x1("free");
              QByteArray y1("dom");
              x1.append(y);
              
              QByteArray x2("free");
              x2.append("dom"); // appending const char*
              

              Both x1 and x2 will contain "freedom" (or more precisely, "freedom\0")

              Conventionally in C, \0 is a marker that means "end of string". So, if you have a char array that contains "free\0dom\0" most C functions will see that as a 4-character string ("free").

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @JKSH said in C to C++, char * and QByteArray - General questions.:

              So, if you have a char array that contains "free\0dom\0" most C functions will see that as a 4-character string ("free").

              Thank you for clarifying. I was aware of the quoted fact, that is mentioned in QByteArray doc. Just how those are concatenated was not clear to me.

              For more information please re-read.

              Kind Regards,
              Artur

              JKSHJ JonBJ 2 Replies Last reply
              0
              • artwawA artwaw

                @JKSH said in C to C++, char * and QByteArray - General questions.:

                So, if you have a char array that contains "free\0dom\0" most C functions will see that as a 4-character string ("free").

                Thank you for clarifying. I was aware of the quoted fact, that is mentioned in QByteArray doc. Just how those are concatenated was not clear to me.

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

                @artwaw said in C to C++, char * and QByteArray - General questions.:

                Just how those are concatenated was not clear to me.

                The easiest way to find out is to write the code and try it :-)

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

                artwawA 1 Reply Last reply
                1
                • artwawA artwaw

                  @JKSH said in C to C++, char * and QByteArray - General questions.:

                  So, if you have a char array that contains "free\0dom\0" most C functions will see that as a 4-character string ("free").

                  Thank you for clarifying. I was aware of the quoted fact, that is mentioned in QByteArray doc. Just how those are concatenated was not clear to me.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @artwaw
                  Mostly, just forget that QByteArray appends an extra \0 at the end. It's mostly for your convenience so it can be read as a string. When appending, ignore it, let QByteArray put an extra \0 at the end for you (and let it overwrite the \0 which was there before).

                  1 Reply Last reply
                  2
                  • JKSHJ JKSH

                    @artwaw said in C to C++, char * and QByteArray - General questions.:

                    Just how those are concatenated was not clear to me.

                    The easiest way to find out is to write the code and try it :-)

                    artwawA Offline
                    artwawA Offline
                    artwaw
                    wrote on last edited by
                    #9

                    @JKSH I know but since I had a few questions I've decided to throw them all into the post. Plus, with my lack of understanding of some basics, it is not guaranteed that I'll be able to spot WHY sample code fails.

                    For more information please re-read.

                    Kind Regards,
                    Artur

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

                      QByteArray is a simple std::vector<char> with an explicit \0 at the end.

                      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
                      • S Offline
                        S Offline
                        Snyder85
                        Banned
                        wrote on last edited by Snyder85
                        #11
                        This post is deleted!
                        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