我試圖讀取二進制文件(例如一個可執行文件)轉換成字符串,然後把它寫回二進制文件字符串
FileStream fs = new FileStream("C:\\tvin.exe", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string myString = enc.GetString(bin);
fs.Close();
br.Close();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] rebin = encoding.GetBytes(myString);
FileStream fs2 = new FileStream("C:\\tvout.exe", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs2);
bw.Write(rebin);
fs2.Close();
bw.Close();
這不起作用(結果具有完全相同的尺寸相同字節,但是不能運行)
,如果我做bw.Write(BIN)的結果是正確的,但我必須將它保存爲一個字符串
做一個十六進制比較,是逆序的字節? (小端到大端) – Amirshk 2010-06-05 11:49:10
爲什麼你需要將它存儲爲字符串? – 2010-06-05 12:09:35