I'm trying to write a byte array to txt file and the result is gibberish instead of my bytes.
This is my function:
public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
try
{
// Open file for reading
System.IO.FileStream _FileStream =
new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
System.IO.FileAccess.Write);
// Writes a block of bytes to this stream using data from
// a byte array.
_FileStream.Write(_ByteArray, 0, _ByteArray.Length);
// close file stream
_FileStream.Close();
return true;
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}",
_Exception.ToString());
}
// error occured, return false
return false;
}
And the result:
"3DUf " E _Xu €ˆ
=2‡¬1p% n Kפ C
_ByteArray?camelCase, no underscores. And returning a bool to indicate success/failure is also frowned upon in .Net. Exceptions are supposed to be the method used to indicate failure. I would just remove that try/catch - let any error bubble up until you can actually meaningfully handle it, even if that's just a top-level handler that logs it and quits._ByteArrayvariable content.