public static string FileReadToBinary(string filename) { FileStream fs = new FileStream(filename, FileMode.Open); Console.WriteLine("File size : " + fs.Length); int fileLength = (int)fs.Length; StringBuilder text = new StringBuilder((int)fs.Length * 8); byte[] bytes = new byte[100]; int startindex = 0; int IsEnd = -1; while (fs.Read(bytes, startindex, bytes.Length) != 0) { if (IsEnd > 0) { } foreach (byte b in bytes) { if (text.Length == fileLength * 8) { break; } text.Append(BaseTransform.FromDeciamlToBinary(b)); } } fs.Close(); return(text.ToString()); }
public static void WriteBinaryToFile(string filename, string binaryText) { FileStream fs = new FileStream(filename, FileMode.Create); StringBuilder sub_text = new StringBuilder(800); byte[] bytes = new byte[100]; int length = 800; for (int i = 0; i <= binaryText.Length / 800; i++) { int remain = binaryText.Length - i * 800; if (remain < 800) { length = remain; } sub_text.Remove(0, sub_text.Length); sub_text.Append(binaryText.Substring(i * 800, length)); for (int j = 0; j < sub_text.Length / 8; j++) { bytes[j] = BaseTransform.FromBinaryToByte(sub_text.ToString().Substring(j * 8, 8)); if (remain < 800) { Console.Write(bytes[j].ToString()); } } fs.Write(bytes, 0, sub_text.Length / 8); } Console.WriteLine(); fs.Close(); }