I'm trying to figure out something while reading a binary file.
On my file there's a long type value at the beginning ('8'), and then, depending on the file, the next value will be either a letter (for example 'E') or another long (for example '5'). My question is: How can I know what kind of data value I'm reading on my file?
My code looks like this:
FileStream streamR = new FileStream(archivo, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(streamR);
Boolean checkNext = false;
Boolean bandHeadr = false;
Boolean bandRank = false;
long archivoPos;
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
if (bandHeadr == false)
{
// HERE IT READS THE FIRST VALUE, ALWAYS A LONG TYPE VALUE
long header = reader.ReadInt64();
data.Add(header);
bandCabecera = true;
}
if (checkNext == false)
{
try
{
// HERE I'M TRYING TO CHECK THE NEXT VALUE, BUT RETURNS AN ASCII CODE IF IT IS A LETTER
int ix = reader.PeekChar();
}
catch
{
// THIS WILL IF THE NEXT VALUE IS ANOTHER LONG
if (bandRank == false)
{
try
{
long rang = reader.ReadInt64();
rangO = rang;
button9.Enabled = false;
}
catch
{
// EMPTY CATCH
}
bandRank = true;
}
}
checkNext = true;
}
}
I'm using PeekChar for the sake of not moving to the next position of the file.
How can I know what kind of data value I'm reading on my file?
You can't. Not from the bytes themselves (as opposed to the file's extension etc.). The bytes are just bytes. What they represent depends on whoever wrote the file.
(You can try to see if it makes sense, such as if it must be an 'A' or an int - if it's not an 'A' - it's an int. But it might also be the first byte of an int that just happens to be the same as the ASCII(?) value of an 'A'.)