Dividing binary into chunks

  • Thread starter Thread starter Akhil963
  • Start date Start date
A

Akhil963

Guest
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?

I want it to read in 0 and 1 so that i can apply further logic in Main() into divided chunks.

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

{

// more code

}

}

Continue reading...
 
Back
Top