Good day. Tell me how to solve a similar problem? Methods seem to be dug up, but there are a lot of nuances, and I would like to hear tips or examples. I work with forms. In the future, the resulting numbers will be processed by two sorting algorithms. (No need to help with this) But how to read the video correctly? Do I need to create an array of bytes of 4 to convert to 32-bit numbers? (Recorded 4 bytes -> recorded a number in an array cell) Or will this class itself divide bytes?

  • And where does WinForms? - VladD
  • I thought in working with forms there would be some special features, but everything turned out to be simple and clear. - Vlad Finni

1 answer 1

Actually, what is the question?

Open the stream and read using BinaryReader . We need 32-bit numbers - use the ReadInt32 method.

To convert from BigEndian, you can use IPAddress.NetworkToHostOrder .

 using (var stream = new FileStream(filename, FileMode.Open)) using (var reader = new BinaryReader(stream)) { while (reader.PeekChar() != -1) { int bigEndian = reader.ReadInt32(); int littleEndian = IPAddress.NetworkToHostOrder(bigEndian); // Используем полученное число ... } } 

Note: file size must be a multiple of 4.

  • as it turned out, it is better (in my case) to bring the second line to this form: using (var reader = new BinaryReader (stream, Encoding.ASCII)) - Vlad Finni
  • @VladFinni - if only numbers are read, the encoding has no meaning. It is taken into account only when reading lines and characters. - Alexander Petrov
  • as it turned out - has. For some reason, the buffer was small with a different encoding, and further than 13 elements in the array did not want to read. - Vlad Finni