C# 为什么0x00字符使System.Diagnostics.Debug.WriteLine不写入新行?

C# 为什么0x00字符使System.Diagnostics.Debug.WriteLine不写入新行?,c#,.net,vb.net,unicode,C#,.net,Vb.net,Unicode,工作得很好。。System.Diagnostics.Debug.WriteLine将输出本身写入新行 Dim hex = "41" Dim text As New System.Text.StringBuilder For i As Integer = 0 To hex.Length - 2 Step 2 text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16))) Next System.Di

工作得很好。。System.Diagnostics.Debug.WriteLine将输出本身写入新行

Dim hex = "41"
    Dim text As New System.Text.StringBuilder
    For i As Integer = 0 To hex.Length - 2 Step 2
        text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
    Next
    System.Diagnostics.Debug.WriteLine(text.ToString)
但这是失败的。。输出本身不是新的行,对此有什么解释

据我所知,System.Diagnostics.Debug.Writeline不是做了这样的事情吗:

 Dim hex = "4100"
    Dim text As New System.Text.StringBuilder
    For i As Integer = 0 To hex.Length - 2 Step 2
        text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
    Next
    System.Diagnostics.Debug.WriteLine(text.ToString)
因此,无论我的输入是什么,它都应该有一个换行符,即使在我的输入中有一个终止的00字符?

我怀疑它不是Debug.WriteLine本身-是Windows控件接收输出,它将U+0000视为字符串的结尾


尝试将TextWriterTraceListener写入到调试侦听器集合的文件中,然后在二进制文件查看器中查看该文件,我怀疑您会看到您认为正在写入的每个字符。

以下是详细信息,描述了此处发生的情况:

net中的字符串类是一个字符数组,以指向数组中第一个字符的指针开始,以特殊的终端char\0结束。 将00转换为字节时,得到0。但是0正好等于终端字符\0

因此Console.Write将忽略第一个\0!之后的所有字符!。不仅控制台会采用这种方式,控制部件也会采用这种方式。例如textBox.Text如果您对其应用了一个包含\0字符的字符串,它将不会显示\0之后的任何字符,包括\0本身

编辑:

据我所知,System.Diagnostics.Debug.Writeline不是做了这样的事情吗:

 Dim hex = "4100"
    Dim text As New System.Text.StringBuilder
    For i As Integer = 0 To hex.Length - 2 Step 2
        text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
    Next
    System.Diagnostics.Debug.WriteLine(text.ToString)
我猜Debug.WriteLine不是以这种方式实现的,否则我们不会注意到这种行为。可能它确实将字符串与新行连接起来,如输入+\n

我可以使用Resharper程序诊断Console.WriteLine。代码是:

System.Diagnostics.Debug.Write(input)
System.Diagnostics.Debug.Write("\n")

因此,它将字符串值与新行连接起来。因此,它将产生相同的效果。

要解决此问题,并且仍然显示空字符,请在打印字符串之前对字符串执行.Replace\0、@\0。

4100字符串是否表示为十六进制值?您发现Windows是用C编写的。C语言将二进制零视为字符串终止符。调试器存在相同的问题,请尝试检查text.ToString。请注意显示字符串末尾缺少的双引号。尝试检查text.ToString是什么意思?这不是我已经在尝试的吗?@shobhonk yep据我所知,是A字符+0x00,System.Diagnostics.Debug.Writeline不是这样做的吗。。请参见我编辑的问题,此处没有空格
System.Diagnostics.Debug.Write(input)
System.Diagnostics.Debug.Write("\n")
[SecuritySafeCritical]
public virtual void WriteLine(string value)
{
    if (value == null)
    {
        this.WriteLine();
    }
    else
    {
        int length = value.Length;
        int num2 = this.CoreNewLine.Length;
        char[] destination = new char[length + num2];
        value.CopyTo(0, destination, 0, length);
        switch (num2)
        {
            case 2:
                destination[length] = this.CoreNewLine[0];
                destination[length + 1] = this.CoreNewLine[1];
                break;

            case 1:
                destination[length] = this.CoreNewLine[0];
                break;

            default:
                Buffer.InternalBlockCopy(this.CoreNewLine, 0, destination, length * 2, num2 * 2);
                break;
        }
        this.Write(destination, 0, length + num2);
    }
}