C# - “\0”是什么意思等于?

发布于 2024-08-22 06:47:11 字数 270 浏览 3 评论 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 技术交流群。

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

发布评论

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

评论(6

小嗷兮 2024-08-29 06:47:11

'\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.

再见回来 2024-08-29 06:47:11

它是一个带有空字符的字符串。较旧的字符串库(如 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.

硬不硬你别怂 2024-08-29 06:47:11

它是一个包含字符“\0”的字符串。 C# 并没有以任何特别的方式对待它 - 它只是 unicode 字符 U+0000。如果你写:

int firstCodePoint = text[0];

那么你会发现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:

int firstCodePoint = text[0];

then you'll find firstCodePoint is 0.

少跟Wǒ拽 2024-08-29 06:47:11
Escape Sequence  
\0    
Character Name 
Null    
Unicode Encoding     
0x0000

请参阅此链接。

Escape Sequence  
\0    
Character Name 
Null    
Unicode Encoding     
0x0000

See this link.

森末i 2024-08-29 06:47:11

长度为 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.

放血 2024-08-29 06:47:11

我刚刚找到了一个很好的例子,其中 \0 非常重要且必要。

假设我们要删除以下代码中最后一个不需要的 ,

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write($"{a},");
}

输入图片这里的描述

如果我们只添加Console.Write("\b\n");如下,

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write($"{a},");
    Console.Write("\b\n");
}

输出仍然是一样的。

但是,如果我们按如下方式添加 \0

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write($"{a},");
    Console.Write("\b\0\n");
} 

不需要的尾随 , 就会消失。

输入图片此处描述

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.

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write(
quot;{a},");
}

enter image description here

If we only add Console.Write("\b\n"); as follows,

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write(
quot;{a},");
    Console.Write("\b\n");
}

The output will be still the same.

But if we add \0 as follows,

for (int i = 0; i < 5; i++)
{
    foreach (var a in "Money")
        Console.Write(
quot;{a},");
    Console.Write("\b\0\n");
} 

The unwanted trailing , vanishes.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文