I am trying to distinguish between "text files" and "binary" files, since I would really like to ignore files with "unreadable" content.
I have a file that I believe is a gzip archive. I am trying to ignore this file by detecting magic numbers / file signature. If I open the file with the Hex editor plugin in Notepad ++, I see that the first three hexadecimal codes are 1f 8b 08 .
However, if I read the file using StreamReader , I am not sure how to get to the original bytes.
using (var streamReader = new StreamReader(@"C:\file")) { char[] buffer = new char[10]; streamReader.Read(buffer, 0, 10); var s = new String(buffer); byte[] bytes = new byte[6]; System.Buffer.BlockCopy(s.ToCharArray(), 0, bytes, 0, 6); var hex = BitConverter.ToString(bytes); var otherhex = BitConverter.ToString(System.Text.Encoding.UTF8.GetBytes(s.ToCharArray())); }
At the end of the using statement, I have the following variable values:
hex: "1F-00-FD-FF-08-00" otherhex: "1F-EF-BF-BD-08-00-EF-BF-BD-EF-BF-BD-0A-51-02-03"
None of them begin with the hexadecimal values specified in Notepad ++.
Is it possible to get the source bytes from the result of reading a file through StreamReader ?
source share