How to read a binary file, convert it into proper text format ?
Hi frndz,
I want to read a binary file with .DAT extension and convert the data into valid ASCII format.
Can someone plz help me on this ?!!
thanx
- Prasadd
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
cfquebPosted Feb 10, 2006, 3:17 PM
BinaryFormatter bf = new BinaryFormatter();
bool IsFromArchive = true;
string temporalfilename = "yourfile.dat";
string m_data ="";
string mp_lasterror = "";
if (IsFromArchive)
{
if (File.Exists(Application.StartupPath + @"\Temp\" + temporalfilename))
{
FileStream fstream = new FileStream(Application.StartupPath + @"\Temp\" + temporalfilename,
FileMode.Open, FileAccess.Read);
try
{
m_data = (string) bf.Deserialize(fstream);
}
catch (Exception ex)
{
mp_lasterror = ex.Message;
}
finally
{
//todo
}
fstream.Close();
}
else
MessageBox.Show("The file" + temporalfilename + " was not found in the path!");
}
Regards
cfquebPosted Feb 10, 2006, 3:07 PM
///
/// Deserialize an object from an array of bytes.
///
/// The array of bytes with the object to deserialize
///
public static object BinaryTo(byte[] SerializedImage)
{
IFormatter formatter;
MemoryStream memStream = null;
Object objectFromBinary = null;
//Cursor saveCursor = Cursor.Current;
//try
//{
//Cursor.Current = Cursors.WaitCursor;
try
{
memStream = new MemoryStream(SerializedImage);
formatter = new BinaryFormatter();
objectFromBinary = formatter.Deserialize(memStream);
}
catch (Exception exception)
{
mp_lasterror = exception.Message;
}
finally
{
if (memStream != null) memStream.Close();
}
//}
//finally
//{
// Cursor.Current = saveCursor;
//}
return objectFromBinary;
}
Regards
CFQüeb