1

I have a byte[] array filled with data. I can't rely on the file extension (from which the array of data comes from) being a reliable indicator of the data type.

That being said, how can I "detect" the type of data that is stored in the byte array?

forloop
  • 147
  • 8
  • 1
    It will depend on how the data is stored in the byte[], if it is xml it might be easy to determine the datatype, if it is json it might be harder and if you used BinarySereialization it might be easy to deserialize it and then call .GetType() on the object – Donald Jansen Sep 10 '15 at 06:08
  • @DonaldJansen I am using: `FileStream fileStream = File.OpenRead(path); Content = new byte[fileStream.Length]; fileStream.Read(Content, 0, Content.Length); fileStream.Close();` to embed the data. It's part of a larger class which stores objects as Serialized JSON files. – forloop Sep 10 '15 at 06:09
  • Well it's byes isn't it? – Vance Shipley Sep 10 '15 at 06:09
  • I don't think it is possible to read type from binary data. – Hari Prasad Sep 10 '15 at 06:11
  • Does your software also create your files ? – Donald Jansen Sep 10 '15 at 06:11
  • @DonaldJansen It does create the files as well. Well, on a case by case basis... Sometimes these files are "attached", meaning, sometimes the file could be an existing file the user has chosen to attach, which then gets converted to byte array then stored in JSON file. – forloop Sep 10 '15 at 06:11
  • 1
    use [FindMimeFromData](http://stackoverflow.com/a/62007/578411) – rene Sep 10 '15 at 06:12
  • What you can then do is something like this, its a method I use to send data over a network. 4 bytes to specify the byte[] size, 4 bytes to specify an enum, x bytes (taken from the first 4 bytes to store the actual data) – Donald Jansen Sep 10 '15 at 06:13

0 Answers0