简体   繁体   English

使用Binarywriter写空字节

[英]Write empty bytes using binarywriter

so i am using binarywriter to write some strings over the original strings, but i need to overwrite the whole string in the binary file, so i am trying to write the new string, and overwrite the rest of the old string with 0x00 (empty bytes), this is what i tried, but not working: 所以我正在使用binarywriter在原始字符串上写一些字符串,但是我需要覆盖二进制文件中的整个字符串,所以我试图写新字符串,并用0x00(空字节)覆盖旧字符串的其余部分),这是我尝试过的方法,但无法正常工作:

bw.Write(enc.GetBytes(listView1.Items[i].SubItems[2].Text + (31 - enc.GetByteCount(listView1.Items[i].SubItems[2].Text)) * '\0'));

31 is the constant int that all old strings can't tresspass (don't ask me, it's a script thingy) 31是所有旧字符串都无法tresspass的常量int(不要问我,这是一个脚本问题)

That's because: 那是因为:

number * '\0'
= number * 0  ('\0' converted to int)
=> always equal to 0

To generate a string filled with \\0 (or any char) of a certain length, use: 要生成一个填充有一定长度的\\0 (或任何char)的字符串,请使用:

new string(`\0`, number)

In your case: 在您的情况下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM