I am trying to divide binary data into small chunks from .txt file using the code below. However, this code is reading 0 and 1 in ASCII code and showing the results in 48 and 49 respectively. How can I fix this?
For Ex: 0101 is being read as 48494849.
- public static IEnumerable<IEnumerable<byte>> ReadByChunk(int chunkSize)
- {
- IEnumerable<byte> result;
- int startingByte = 0;
- do
- {
- result = ReadBytes(startingByte, chunkSize);
- startingByte += chunkSize;
- yield return result;
- } while (result.Any());
- }
- public static IEnumerable<byte> ReadBytes(int startingByte, int byteToRead)
- {
- byte[] result;
- using (FileStream stream = File.Open(@"C:\Users\file.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
- using (BinaryReader reader = new BinaryReader(stream))
- {
- int bytesToRead = Math.Max(Math.Min(byteToRead, (int)reader.BaseStream.Length - startingByte), 0);
- reader.BaseStream.Seek(startingByte, SeekOrigin.Begin);
- result = reader.ReadBytes(bytesToRead);
- }
- return result;
- }
- public static void Main()
- {
- foreach (IEnumerable<byte> bytes in ReadByChunk(chunkSize))
- {
-
- }
- }