I'm converting a delphi app to C#. There are a bunch of packed records, and according to a similar question I asked a few weeks ago, it would be better to convert to classes. However, I'm told I need to convert them to structs, and I could use some help. I'm going to be using a BinaryReader
to read from a file and assign values to the fields inside of the structs.
*Note, the file I'm reading from was made using Delphi and packed records.
Here's an example struct:
Delphi:
Testrec = packed record
now: TDateTime;
MinLat: longint;
MinLong: longint;
Firsttime: TDateTime;
MinAlt: single;
MinFirst: single;
MinDepth: single;
MinSpeed: single;
MinBot: single;
res3: single;
res4: single;
res5: single;
res6: single;
MaxLat: longint;
MaxLong: longint;
Lasttime: TDateTime;
MaxAlt: single;
MaxFirst: single;
MaxDepth: single;
MaxSpeed: single;
MaxBot: single;
res9: single;
res10: single;
res11: single;
res12: single;
DataFlags: longint;
ReviewFlags: longint;
res13: longint;
FirstPost: longint;
end;
Here's my C# version:
public struct Testrec
{
double now;
int MinLat;
int MinLong;
double Firsttime;
float MinAlt;
float MinFirst;
float MinDepth;
float MinSpeed;
float MinBot;
float res3;
float res4;
float res5;
float res6;
int MaxLat;
int MaxLong;
double Lasttime;
float MaxAlt;
float MaxFirst;
float MaxDepth;
float MaxSpeed;
float MaxBot;
float res9;
float res10;
float res11;
float res12;
int DataFlags;
int ReviewFlags;
int res13;
int FirstPost;
}
Do I need to do a StructLayout
, Size
, and CharSet
?
Edit: Here's the relevant delphi code regarding the reading of the binary file:
Testrec Header;
HeaderSize = 128;
RampStream:=TFileStream.Create(FilePath,fmOpenReadWrite OR fmShareExclusive );
RampStream.Read(Header,HeaderSize);
StartTime:=Header.Firsttime;
EndTime:=Header.Lasttime;
Here's how I set up my Binary Reader:
RampStream = new BinaryReader(new FileStream(RampName, FileMode.Open, FileAccess.ReadWrite, FileShare.None));
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…