Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I've a fixed size of binary file. This file was contain 96 record with different type like a struct below.
file : "exam.dat"
C#
public struct Exam
{
    double  data1;
    double  data2;
    single  data3;
    int     data4;
    double  data5;
}

public Exam[] record = new Exam[96];     



I can read it by using binaryReader or streamreader and data keep in byte array format, how can i copy into array of struct.
Posted
Updated 8-May-13 3:07am
v2

A simple approach would be providing a contructor for your struct accepting an array of bytes. Such a conctructor would use repeatedly the BitConverter class[^] (for instance you may use data1 = BitConverter.ToDouble( ba, 0); where ba is your byte array).
 
Share this answer
 
You can use a converter.

For double:

C#
var index = 0;
Exam[index].data1 = BitConverter.ToDouble(byteArr, 0);
//do it for data2, data3 . . .
index++;
. . .
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900