A
A
Alexander Prokopenko2022-02-03 04:16:01
C++ / C#
Alexander Prokopenko, 2022-02-03 04:16:01

How to extract data from bytes in C#?

Good afternoon, I have an encrypted file, I need to read it byte by byte and extract certain data from them. How can i do this? And also to understand what data to extract, there is a certain letter in front of the characters. For example, after D, I need to read 4 bytes and assemble them into a 32-bit number.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@insighter, 2022-02-03
@AlexanderProkopenko

const int MIN_DATA_SIZE = 1 + sizeof(int);
        const byte D_PREFIX = 0x44; // D ascii код

        static void Main(string[] args)
        {
            {
                using var reader = new BinaryReader(File.OpenRead(@"c:\temp\file.dat"));
                while (true) {
                    var canRead = reader.BaseStream.Position <= reader.BaseStream.Length - MIN_DATA_SIZE;
                    if (!canRead) 
                        break;

                    if (reader.ReadByte() != D_PREFIX) 
                        continue;

                    var value = reader.ReadInt32();
                    Console.WriteLine($"Прочитано: {value}");
                }
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question