C# - “\0”是什么意思等于?
我正在使用 Pex ,它传递到我的方法中的参数之一是<代码>“\0”。
这意味着什么?根据我的方法的内容,我的猜测是一个空字符串 (""
)。但是,如果相同,为什么不直接使用 ""
而不是 "\0"
呢?
有人知道它是什么吗?
I am playing with Pex and one of the parameters it passes into my method is "\0"
.
What does that mean? My guess is an empty string (""
) based on the content of my method. However, if it is the same then why not just use ""
instead of "\0"
?
Anyone know what it is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
'\0' 是“空字符”。它用于终止 C 和 C++ 的某些部分中的字符串。 Pex 正在进行测试以查看您的代码如何处理空字符,可能会寻找 Poison Null字节安全漏洞。
大多数 C# 代码无需担心;但是,如果将字符串传递给非托管代码,则可能会遇到问题。
编辑:
明确地说... Pex 正在传递一个包含空字符的字符串。这不是空引用。
'\0' is a "null character". It's used to terminate strings in C and some portions of C++. Pex is doing a test to see how your code handles the null character, likely looking for the Poison Null Byte security exploit.
Most C# code has nothing to fear; if you pass your string to unmanaged code, however, you may have problems.
Edit:
Just to be explicit... Pex is passing a string containing a null character. This is not a null reference.
它是一个带有空字符的字符串。较旧的字符串库(如 C 或较旧的 C++ 库中使用的字符串库)使用“\0”字符来指示字符串的结尾。
.Net 等较新的环境使用不同的系统,但有很多关于以 '\0' 结尾字符串的历史,因此它是一个常见的错误点。像 Pex 这样的测试库将使用它来确保您的程序正确处理它。
It's a string with a null character. Older string libraries — like that used in C or older C++ libraries — used the '\0' character to indicate the end of the string.
Newer environments like .Net use a different system, but there is a lot of history around ending a string with '\0', such that it's a common point of error. Testing libraries like Pex will use it to make sure your program handles it correctly.
它是一个包含字符“\0”的字符串。 C# 并没有以任何特别的方式对待它 - 它只是 unicode 字符 U+0000。如果你写:
那么你会发现
firstCodePoint
是0。It's a string containing the character '\0'. C# doesn't treat this in any particularly special way - it's just unicode character U+0000. If you write:
then you'll find
firstCodePoint
is 0.请参阅此链接。
See this link.
长度为 1 的字符串,包含字符 \u0000(又名 NUL)。这个角色没有被特殊对待。
在 C 中,使用 \0 来终止字符串,您还分配了一个长度为 1 的字符串。在这种情况下,标准字符串函数将报告长度为 0,因为该字符串包含 \0 并以其终止。您可以安全地修改 str[0],或 strncat 将单个字符放入其中。
A string of length 1, containing the character \u0000 (aka NUL). This character is not treated specially.
In C, which uses \0 to terminate string, you also allocate a string of length 1. In this case the standard string functions will report a length of 0, since the string contains \0 as well as being terminated with it. You could safely modify str[0], or strncat a single character into it.
我刚刚找到了一个很好的例子,其中
\0
非常重要且必要。假设我们要删除以下代码中最后一个不需要的
,
。如果我们只添加
Console.Write("\b\n");
如下,输出仍然是一样的。
但是,如果我们按如下方式添加
\0
,不需要的尾随
,
就会消失。I just found a good example in which
\0
is very important and necessary.Assume we want to remove the last unwanted
,
in the following code.If we only add
Console.Write("\b\n");
as follows,The output will be still the same.
But if we add
\0
as follows,The unwanted trailing
,
vanishes.