I wanna know how it is possible to read a file in binary format.
for example a tiff image file may have the following binary format in hex 0000 4949 002A 0000. how can i get these values in c#?
I wanna know how it is possible to read a file in binary format.
for example a tiff image file may have the following binary format in hex 0000 4949 002A 0000. how can i get these values in c#?
Here is how I usually read files in hexadecimal format, changed for the header, as you need:
using System;
using System.Linq;
using System.IO;
namespace FileToHex
{
class Program
{
static void Main(string[] args)
{
//read only 4 bytes from the file
const int HEADER_SIZE = 4;
byte[] bytesFile = new byte[HEADER_SIZE];
using (FileStream fs = File.OpenRead(@"C:\temp\FileToHex\ex.tiff"))
{
fs.Read(bytesFile, 0, HEADER_SIZE);
fs.Close();
}
string hex = BitConverter.ToString(bytesFile);
string[] header = hex.Split(new Char[] { '-' }).ToArray();
Console.WriteLine(System.String.Join("", header));
Console.ReadLine();
}
}
}
You can use the ReadAllBytes method of the System.IO.File class to read the bytes into an array:
System.IO.FileStream fs = new System.IO.FileStream(@"C:\Temp\sample.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read);
int size = 1024;
byte[] b = new byte[size];
fs.Read(b, 0, size);
I have not used LibTIFF.Net, http://bitmiracle.com/libtiff but it seems to be fairly complete.
Using it, instead of reading the file as bytes and then decoding the header(s) may be a lot easier for you.