0

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.

3
  • If you know it will be a number or a letter then you could try TryParse and if it succeeds then you have it saved as a number if it fails then save it as a char. Commented Oct 17, 2017 at 18:45
  • If you can use C#7 then you could try a Switch statement with patterns. MSDN Commented Oct 17, 2017 at 18:46
  • Also, keep in mind, there is no text but encoded text. When you get bytes for text, you must also get the encoding they were written with. And, there is not one multi-byte integer representation but two: little-endian and big-endian. All this must be communicated in a file format. Commented Oct 17, 2017 at 22:11

1 Answer 1

2

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'.)

Sign up to request clarification or add additional context in comments.

3 Comments

Spot on. Maybe worth mentioning that binary files usually address that lack of clarity by having defined formats to give the reader a hint
Then the best method will be try to read the second value as a 'char'? Because I did that and it still reads it regardless.
@CodeXtack I'm sorry. I don't understand your comment so i can't answer it. But anyway, see Gus's comment above yours - The point is that the correct way is for the creator of the file to have that information communicated to you. This can be done, for example, by some byte being a flag for whether the byte after it will be a char or a long.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.