如何将终止 NUL 字符 \x00 放入字符串中?
由于 \x00 也是终止字符,因此如何获得带有空字符 \x00 的字符串?
我确实需要它作为我的计划的一部分。
我需要的字符串是“\x00\x00\x00\x00”。它有一些特殊的语法吗?它是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 C++ 中,
std::string
类在包含 NUL 字符时可以工作。但是:
c_str()
函数将会失败。const char*
的构造函数将会失败。使用
std::vector
可能会为您提供更好的服务。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>
.此构造函数告诉 string 使用 char* 的前 4 个字符,而不将任何 \0 字符解释为 null。
This constructor tells string to use the 1st 4 characters of the char*, without interpreting any \0 characters as null.
你做对了。您只需避免将 null 解释为结束字符。
但你怎么知道它的终点在哪里呢?我不知道;您可以将长度存储在某处。
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.
您可以有一个嵌入空值的“正常”字符串,但在第一个空值时,任何期望以空值结尾的字符串的函数都将停止处理它;因此,您需要使用计数字符串。
C++
std::string
是一种计数字符串类型,您可以使用它来携带这些字符串。但请记住,在使用它时不应将其转换回 C 字符串(即不要使用c_str()
方法),否则您将回到原点。但是,要获得更具体的建议,您应该更好地解释您想要实现的目标。
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 (i.e. don't use thec_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.
如果它是你想要的 std::string ,你可以得到任何你想要的大小,
给你一个包含 10 个 nul 字符的字符串。
If it is a std::string you want, you can get any size you want
gives you a string with 10 nul characters.