如何从包含 \0 的字符串中获取 C 字符串而不丢失 \0

发布于 2024-10-03 19:47:25 字数 314 浏览 12 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仙女 2024-10-10 19:47:25

您无法创建包含 '\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, while mystring.c_str() always returns a zero-terminated C-string.

情深缘浅 2024-10-10 19:47:25

这是不可能的。 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.

空城旧梦 2024-10-10 19:47:25

嗯,myString 可能在构造/赋值时被截断。您可以尝试使用两个迭代器作为参数的 std::basic_string::assign ,或者简单地使用 std::vector,后者在您的使用中更常见案件。
并且您采用该 C 字符串的 API 实际上必须支持采用 char 指针和长度。

Well, myString has probably been truncated at construction/assignment time. You can try std::basic_string::assign which takes two iterators as arguments or simply use std::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.

七分※倦醒 2024-10-10 19:47:25

我有点困惑,但是:

string x("abc");
if (x.c_str()[3] == '\0')
{ cout << "there it is" << endl; }

I'm a bit confused, but:

string x("abc");
if (x.c_str()[3] == '\0')
{ cout << "there it is" << endl; }
云朵有点甜 2024-10-10 19:47:25

这可能无法满足您的需求,您确实说过“那些 \0 是必不可少的”,但是转义或替换“\0”字符怎么样?

这些想法之一可行吗?

  1. 将“\0”字符替换为“\t”(制表符,十进制 9)。
  2. 将 '\0' 替换为一些很少使用的字符值,例如十进制 1 或十进制 255。
  3. 创建转义码,例如将每个 '\0' 字符替换为编码子字符串(如“\000”中的八进制)。 (请务必将任何原始的“\”也替换为编码值(例如“\134”))。

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?

  1. replace the '\0' chars with a '\t' (tab char, decimal 9).
  2. replace the '\0' with some rarely used char value like decimal 1, or decimal 255.
  3. Create an escape code, say by replacing each '\0' char with a coded substring, (like octal as in "\000"). (Be sure to replace any original '\' with a coded value as well (like "\134")).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文