C# - "\0" 等同于什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2292850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:09:31  来源:igfitidea点击:

C# - What does "\0" equate to?

c#stringescapingpexpex-and-moles

提问by Vaccano

I am playing with Pexand one of the parameters it passes into my method is "\0".

我正在玩Pex,它传递给我的方法的参数之一是"\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"?

这意味着什么?""根据我的方法的内容,我的猜测是一个空字符串 ( )。但是,如果它是相同的,那么为什么不直接使用""而不是"\0"

Anyone know what it is?

有人知道这是什么吗?

采纳答案by Randolpho

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

'\0' 是一个“空字符”。它用于终止 C 和 C++ 的某些部分中的字符串。Pex 正在进行测试以查看您的代码如何处理空字符,可能是在寻找Poison Null Byte安全漏洞。

Most C# code has nothing to fear; if you pass your string to unmanaged code, however, you may have problems.

大多数 C# 代码没有什么可害怕的;但是,如果将字符串传递给非托管代码,则可能会遇到问题。

Edit:

编辑:

Just to be explicit... Pex is passing a string containing a null character. This is nota null reference.

只是为了明确...... Pex 正在传递一个包含空字符的字符串。这不是空引用。

回答by Jon Skeet

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:

它是一个包含字符 '\0' 的字符串。C# 不会以任何特别特殊的方式处理它——它只是 unicode 字符 U+0000。如果你写:

int firstCodePoint = text[0];

then you'll find firstCodePointis 0.

然后你会发现firstCodePoint是0。

回答by Joel Coehoorn

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.

它是一个带有空字符的字符串。较旧的字符串库(如在 C 或较旧的 C++ 库中使用的库)使用 '\0' 字符来指示字符串的结尾。

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.

.Net 等较新的环境使用不同的系统,但有很多关于以 '\0' 结束字符串的历史,因此它是一个常见的错误点。像 Pex 这样的测试库将使用它来确保您的程序正确处理它。

回答by Lachlan Roche

A string of length 1, containing the character \u0000 (aka NUL). This character is not treated specially.

长度为 1 的字符串,包含字符 \u0000(又名 NUL)。这个字符没有被特殊对待。

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.

在 C 中,使用 \0 终止字符串,您还分配一个长度为 1 的字符串。在这种情况下,标准字符串函数将报告长度为 0,因为该字符串包含 \0 并且以它结尾。您可以安全地修改 str[0],或将单个字符插入其中。

回答by Zoba

Escape Sequence  
##代码##    
Character Name 
Null    
Unicode Encoding     
0x0000

See this link.

请参阅此链接。