2013-07-18 48 views
0

所以我使用二进制写入一些字符串在原始字符串,但我需要覆盖二进制文件中的整个字符串,所以我想写新字符串,并覆盖其余的旧的字符串使用0x00(空字节),这是我尝试过,但没有工作:使用二进制写入空字节

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

31是中恒INT所有的旧字符串不能tresspass(不要问我,这是一个脚本啄)

回答

5

这是因为:

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))))); 
+0

谢谢你,它的工作就像魅力:) – Omarrrio

+1

你很受欢迎。 – manji