0

I have source code available for a legacy Delphi application that creates/reads a binary file. I have to read that binary file in a C# application.

The code in Delphi is something like this :

fs := TFileStream.Create(dst,fmOpenRead);
      try           
       while fs.Position<fs.Size do begin
         fs.Read(sSomeVar,sizeof(dDateTime));
         fs.Read(sSomeVar1,sizeof(sIntValue));
         fs.Read(sSomeVar2,sizeof(sCardinalValue1));
         fs.Read(sSomeVar3,sizeof(sStringValue));
        ....

It appears that I have read the entire file into a byte[] and then interpret number of bytes to correct data type(for e.g. integer, string, uint1)?

I couldn't find a c# example anywhere.

Thanks,

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
user2005803
  • 73
  • 2
  • 9
  • 1
    did you look anywhere? https://stackoverflow.com/questions/2030847/best-way-to-read-a-large-file-into-a-byte-array-in-c https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter?view=netframework-4.7.2 – Charles Oct 06 '18 at 16:24
  • Thanks. I will give it a try. – user2005803 Oct 06 '18 at 16:33
  • 1
    No, you don't need to read the entire file into a byte array – David Heffernan Oct 06 '18 at 17:17
  • David, Do u have any other way of reading individual data types from the file? My first datatype is datetime so how do I convert 8 raw bytes to Datetime type? – user2005803 Oct 06 '18 at 17:39
  • 2
    Read a double.... – David Heffernan Oct 06 '18 at 20:11
  • Thank you David and Charles! Got it working. – user2005803 Oct 06 '18 at 21:31
  • 4
    just curious: what makes this Delphi application "legacy"? What is the reason for re-implement working code? Don't you think, that your lack of knowledge of Delphi details could be more dangerous for the "new" and "modern" C# version, than "legacy" load for the old code? Or maybe your boss cannot see that? ;) – zdzichs Oct 07 '18 at 14:39
  • @zdzichs: I get what you're saying, but it is not an unusual situation for a company to transition to a new language/toolset. I can imagine that a company might decide that it is easier to find and hire good .Net developers as opposed to Delphi developers, and as a result all new development would be done in .Net. Once this transition has happened, code that is not in the newly chosen language starts to become more of a liability, as the existing team becomes less able to support it when issues arise. It's better to convert the code while the team still has the knowledge/ability to do so. – Sean Worle Jul 03 '19 at 21:47

0 Answers0