How would i get a string with the null character \x00 since \x00 is also the terminating character?
I really do need it for part of my program.
The string I need is "\x00\x00\x00\x00". Is there some special syntax for it? What is it?
In C++ the std::string
class will work while including NUL characters.
However:
c_str()
function will fail. const char*
will fail. You would probably be better served by using a std::vector<char>
.
You're doing it correctly. You just need to avoid interpreting a null as the ending character.
But then how do you know where it ends? I don't know; you could store the length somewhere instead.
You can have a "normal" string which embeds nulls, but at the first null any function that expects a null-terminated string will stop processing it; thus, you need to use counted strings.
C++ std::string
being a type of counted string, you can use it to carry around these strings. Keep in mind however that you shouldn't convert it back to a C string when using it (ie don't use the c_str()
method), otherwise you will be back to square one.
However, to have more specific suggestions, you should explain a bit better what are you trying to achieve.
If it is a std::string you want, you can get any size you want
std::string s(10, '\0');
gives you a string with 10 nul characters.
std::string str("\0\0\0\0", 4);
This constructor tells string to use the 1st 4 characters of the char*, without interpreting any \0 characters as null.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.