如何从包含 \0 的字符串中获取 C 字符串而不丢失 \0
我目前有一个相当大的字符串。我需要将其转换为 C 字符串 (char*),因为我想使用的函数仅采用 C 字符串作为参数。
我的问题是,我尝试的任何操作都会使最终的 C 字符串 wayyy 小于原始字符串,因为我的字符串包含许多 \0。那些 \0 是必不可少的,所以我不能简单地删除它们:(...
我尝试了各种方法来做到这一点,但最常见的是:
myString.c_str();
myString.data();
不幸的是,C 字符串始终只是之前的原始字符串的内容第一个 \0.
任何帮助将不胜感激!
I currently have a pretty huge string. I NEED to convert it into a C-string (char*), because the function I want to use only take C-string in parameter.
My problem here is that any thing I tried made the final C-string wayyy smaller then the original string, because my string contains many \0. Those \0 are essential, so I can't simply remove them :(...
I tried various way to do so, but the most common were :
myString.c_str();
myString.data();
Unfortunately the C-string was always only the content of the original string that was before the first \0.
Any help will be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法创建包含
'\0'
字符的 C 字符串,因为根据定义,C 字符串是以'\0'
结尾的字符序列 (也称为“以零结尾的字符串”),因此序列中的第一个'\0'
结束该字符串。但是,有些接口采用指向第一个字符和字符序列长度的指针。这些可能能够处理包括
'\0'
在内的字符序列。请注意
myString.data()
,因为它返回一个指向可能不是以零结尾的字符序列的指针,而mystring.c_str()
始终返回零- 终止 C 字符串。You cannot create a C-string which contains
'\0'
characters, because a C-string is, by definition, a sequence of characters terminated by'\0'
(also called a "zero-terminated string"), so the first'\0'
in the sequence ends the string.However, there are interfaces that take a a pointer to the first character and the length of the character sequence. These might be able to deal with character sequences including
'\0'
.Watch out for
myString.data()
, because this returns a pointer to a character sequence that might not be zero-terminated, whilemystring.c_str()
always returns a zero-terminated C-string.这是不可能的。 null 是一个以 null 结尾的字符串的结尾。如果您查看字符缓冲区(使用
&myString[0]
),您会发现 NULL 仍然存在。但是,任何 C 函数都无法正确解释这些 NULL,因为 NULL 不是 C 中字符串中间的有效值。This is not possible. The null is the end of a null terminated string. If you take a look at your character buffer (use
&myString[0]
), you'll see that the NULLs are still there. However, no C functions are going to interpret those NULLs correctly because NULL is not a valid value in the middle of a string in C.嗯,,后者在您的使用中更常见案件。
myString
可能在构造/赋值时被截断。您可以尝试使用两个迭代器作为参数的 std::basic_string::assign ,或者简单地使用 std::vector并且您采用该 C 字符串的 API 实际上必须支持采用 char 指针和长度。
Well,
myString
has probably been truncated at construction/assignment time. You can trystd::basic_string::assign
which takes two iterators as arguments or simply usestd::vector <char>
, the latter being more usual in your use case.And your API taking that C string must actually support taking a char pointer together with a length.
我有点困惑,但是:
I'm a bit confused, but:
这可能无法满足您的需求,您确实说过“那些 \0 是必不可少的”,但是转义或替换“\0”字符怎么样?
这些想法之一可行吗?
This may not meet your needs, you did say 'Those \0 are essential', but how about escaping or replacing the '\0' chars?
Would one of these ideas work?