2013-07-18 51 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