ReadAllBytes to fill an array of floats ?

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

Is there a way to read an entire file into a float array ?

Suppose 1,000 floats (arrayed 250 rows by 4 columns) are stored in a
4,000 byte file.

Starting from
byte[] data = File.ReadAllBytes ("floatdump.data");

How would I cast or overlay a float array interpretation of the byte
array ?

Thanks!
 
H

Harlan Messinger

Richard said:
Is there a way to read an entire file into a float array ?

Suppose 1,000 floats (arrayed 250 rows by 4 columns) are stored in a
4,000 byte file.

Starting from
byte[] data = File.ReadAllBytes ("floatdump.data");

How would I cast or overlay a float array interpretation of the byte
array ?

You can skip the byte array. For an unknown number of floats:

BinaryReader reader = new BinaryReader(new MemoryStream(data));
int floatCount = reader.BaseStream.Length / 4;

float[] floats = new float[floatCount];
for (int i = 0; i < floatCount; i++)
floats = reader.ReadSingle();

reader.Close();

But you can also skip the byte array step:

BinaryReader reader =
new BinaryReader(File.Open("floatdump.data", FileMode.Open));
 

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