Include nulls in buffer

I am having trouble putting together a buffer to send over a network socket that includes nulls. Basically what I want to do is:

char* buffer = argv[3] + argv[4] + argv[5];

Where buffer ends up containing the three null separated strings.

Thank you in advance for your assistance.
You could try something like this:
1
2
3
std::string buffer = std::string() + argv[3] + '\0' + argv[4] + '\0' + argv[5] + '\0';

send(socket, buffer.c_str(), buffer.length(), 0); // or something similar 
closed account (zb0S216C)
Once a null-character is found within a string, the trailing characters are ignored. Of course, this only applies when reading from the string.

Wazzak
That only applies to null-terminated strings, a.k.a. C strings and also depends on how you read them.
Last edited on
+1 Galika nd Athar.

std::string is perfectly capable of containing null characters. You just have to be mindful of how you insert them.
I somewhat recently had exactly the same problem (using winhhtp). I eventually redid much of the surrounding code with naked pointers and memcpy. But (I worked out later) the issue was not with string but with an implicit conversion that was occuring along the way, which truncated the final result at the first null character.
Last edited on
That worked perfectly Galik. Thank you.
Topic archived. No new replies allowed.