Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用binarywriter写入空字节_C#_Winforms_Binarywriter - Fatal编程技术网

C# 使用binarywriter写入空字节

C# 使用binarywriter写入空字节,c#,winforms,binarywriter,C#,Winforms,Binarywriter,因此,我使用binarywriter在原始字符串上写入一些字符串,但我需要覆盖二进制文件中的整个字符串,因此我尝试写入新字符串,并用0x00(空字节)覆盖旧字符串的其余部分,这是我尝试的,但不起作用: bw.Write(enc.GetBytes(listView1.Items[i].SubItems[2].Text + (31 - enc.GetByteCount(listView1.Items[i].SubItems[2].Text)) * '\0')); 31是所有旧字符串都不能传递的常量

因此,我使用binarywriter在原始字符串上写入一些字符串,但我需要覆盖二进制文件中的整个字符串,因此我尝试写入新字符串,并用0x00(空字节)覆盖旧字符串的其余部分,这是我尝试的,但不起作用:

bw.Write(enc.GetBytes(listView1.Items[i].SubItems[2].Text + (31 - enc.GetByteCount(listView1.Items[i].SubItems[2].Text)) * '\0'));
31是所有旧字符串都不能传递的常量int(别问我,这是一个脚本)

这是因为:

number * '\0'
= number * 0  ('\0' converted to int)
=> always equal to 0
要生成填充有特定长度的
\0
(或任何字符)的字符串,请使用:

new string(`\0`, number)
就你而言:

string newStr = listView1.Items[i].SubItems[2].Text;
bw.Write(enc.GetBytes(newStr + new string('\0', (31 - enc.GetByteCount(newStr)))));
那是因为:

number * '\0'
= number * 0  ('\0' converted to int)
=> always equal to 0
要生成填充有特定长度的
\0
(或任何字符)的字符串,请使用:

new string(`\0`, number)
就你而言:

string newStr = listView1.Items[i].SubItems[2].Text;
bw.Write(enc.GetBytes(newStr + new string('\0', (31 - enc.GetByteCount(newStr)))));