C# 什么是二进制空字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1488060/
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-06 18:01:27  来源:igfitidea点击:

What is a binary null character?

c#

提问by JL.

I have a requirement to create a sysDesk log file. In this requirement I am supposed to create an XML file, that in certain places between the elements contains a binary null character.

我需要创建一个 sysDesk 日志文件。在这个要求中,我应该创建一个 XML 文件,在元素之间的某些地方包含一个二进制空字符。

Can someone please explain to me, firstly what is a binary null character, and how can I write one to a text file?

有人可以向我解释一下,首先什么是二进制空字符,以及如何将一个写入文本文件?

采纳答案by Jon Skeet

I suspect it means Unicode U+0000. However, that's not a valid character in an XML file... you should see if you can get a very clear specification of the file format to work out what's actually required. Sample files would also be useful :)

我怀疑这意味着 Unicode U+0000。但是,这不是 XML 文件中的有效字符……您应该查看是否可以获得非常明确的文件格式规范,以计算出实际需要的内容。示例文件也很有用 :)

Comments are failing me at the moment, so to address a couple of other answers:

目前评论让我失望,所以要解决其他几个答案:

  • It's not a string termination character in C#, as C# doesn't use null-terminated strings. In fact, all .NET strings arenull-terminated for the sake of interop, but more importantly the length is stored independently. In particular, a C# string can entirely validlyinclude a null character without terminating it:

    string embeddedNull = "a
    string justNullString = "
    string embeddedNull = "a
    string justNullString = "
    System.Text.Encoding.ASCII.GetChars(new byte[] {00});
    
    "; char justNullChar = '##代码##';
    b"; Console.WriteLine(embeddedNull.Length); // Prints 3
    "; char justNullChar = '##代码##';
    b"; Console.WriteLine(embeddedNull.Length); // Prints 3
  • The method given by rwmnau for getting a null character or string is very inefficient for something simple. Better would be:

    ##代码##
  • 它不是 C# 中的字符串终止字符,因为 C# 不使用以空字符结尾的字符串。实际上,为了互操作,所有 .NET 字符串都以空字符结尾,但更重要的是长度是独立存储的。特别是,C# 字符串可以完全有效地包含空字符而不终止它:

    ##代码##
  • rwmnau 给出的获取空字符或字符串的方法对于简单的事情来说效率很低。更好的是:

    ##代码##

回答by Mark Rushakoff

A binary null character is just a char with an integer/ASCII value of 0.

二进制空字符只是一个整数/ASCII 值为 0 的字符。

You can create a null character with Convert.ToChar(0)or the more common, more well-recognized '\0'.

您可以使用Convert.ToChar(0)或更常见、更容易识别的'\0'.

回答by SqlRyan

A binary NULL character is one that's all zeros (0x00 in Hex). You can write:

二进制 NULL 字符是全零(十六进制为 0x00)的字符。你可以写:

##代码##

to get it in C#.

在 C# 中获取它。

回答by Joren

The null character is the special character that's represented by U+0000 (encoded by all-zero bits). The null character is represented in C# by the escape sequence \0, as in "This string ends with a null character.\0".

空字符是由 U+0000 表示的特殊字符(由全零位编码)。空字符在 C# 中由转义序列表示\0,如"This string ends with a null character.\0".