TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ram Prasad
NA
326
18.2k
dividing binary data into smaller chunks
Jul 21 2020 5:10 PM
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))
{
// more code
}
}
Reply
Answers (
4
)
Microsoft Power Platform - Power Apps Portal
Capture Crop and upload Image