C to C++, char * and QByteArray - General questions.
-
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 betweenchar *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\0if I am not mistaken, so it is a matter of +1 here and there... But if I am to call a method that acceptschar *and also requiressizeOf()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 likestrlcat- 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 viaQByteArray::append()- maybe a silly question but I found docs inconclusive: if I am to appendchar *(null terminated) to already null terminated QByteArray that way - will the trailing\0be removed before append or left and I'll end up with two\0in 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.
-
@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."
-
@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."
@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.
-
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.
-
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 betweenchar *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\0if I am not mistaken, so it is a matter of +1 here and there... But if I am to call a method that acceptschar *and also requiressizeOf()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 likestrlcat- 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 viaQByteArray::append()- maybe a silly question but I found docs inconclusive: if I am to appendchar *(null terminated) to already null terminated QByteArray that way - will the trailing\0be removed before append or left and I'll end up with two\0in 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.
@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\0be removed before append or left and I'll end up with two\0in the array?You won't end up with two
\0in 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
x1andx2will contain "freedom" (or more precisely,"freedom\0")Conventionally in C,
\0is 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"). -
@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\0be removed before append or left and I'll end up with two\0in the array?You won't end up with two
\0in 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
x1andx2will contain "freedom" (or more precisely,"freedom\0")Conventionally in C,
\0is 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").@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.
-
@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.
@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 :-)
-
@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.
@artwaw
Mostly, just forget thatQByteArrayappends an extra\0at the end. It's mostly for your convenience so it can be read as a string. When appending, ignore it, letQByteArrayput an extra\0at the end for you (and let it overwrite the\0which was there before). -
@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 :-)
-
QByteArray is a simple std::vector<char> with an explicit \0 at the end.