loading a file to an array in 8 bit pieces

F

Flemming Hansen

Hi guys,

I want to load a binary file in to an array by splitting the file in 8 bit
pieces. I mean:

FileStream s = new FileStream(path, FileMode.OpenOrCreate); // Open

StreamReader r = new StreamReader(s);

string data = r.ReadToEnd();

r.Close();

the file is loaded to the string "data" as a long string, and I want to
split "data" in 8 bit pieces and save it in an array

what's the easiest way of doing it?
 
J

Jon Skeet [C# MVP]

Flemming Hansen said:
I want to load a binary file in to an array by splitting the file in 8 bit
pieces. I mean:

FileStream s = new FileStream(path, FileMode.OpenOrCreate); // Open

StreamReader r = new StreamReader(s);

string data = r.ReadToEnd();

r.Close();

the file is loaded to the string "data" as a long string, and I want to
split "data" in 8 bit pieces and save it in an array

what's the easiest way of doing it?

Do you really mean 8 bit pieces, i.e. bytes? If so, just use Stream and
read from it. See http://www.pobox.com/~skeet/csharp/readbinary.html
for a way to reliably read the entire contents of a stream.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top