1

I want to transition some old files first to human readable type, so in Delphi code is following:

OpenFileWriteRA(MyF, dd+'invoice.mfs', SizeOf(TFSerialDocEx)) then

and then calling

ReadFile(MyF, vSS1, SizeOf(TFSerialDocEx), nr1, nil);

So i am looking for a way to conver this files using with small programm i want to make it with C#, as i am more fammiliar with C# than with Delphi. .MFS file is written in binary, so what would i need to convert this to text/string, i tryed with simple binary convert but it was not ok, as it seems SizeOf Object at paramters is big thing here or?

5
  • Same way you would with c# to be honest - read the structures in, write them out in a human readable way Commented Mar 15, 2016 at 7:38
  • So i should first transfer object (or struct?) to C#? And then try to read them ? Commented Mar 15, 2016 at 7:40
  • No, Id stick with delphi reading them. Commented Mar 15, 2016 at 7:43
  • TFSerialDocEx is most likely a record. But why do we have to guess? Commented Mar 15, 2016 at 7:43
  • Please include the definition into your question, and show us what you tried. Commented Mar 15, 2016 at 7:47

1 Answer 1

4

There broadly speaking are three approaches that I would consider:

1. Transform data with Delphi code

Since you already have Delphi code to read the data, and structures defined, it will be simplest and quickest to transform the data with Delphi code. Simply read it using your existing code and then output in human readable form. For instance using the built in JSON libraries.

2. Define an equivalent formatted C# structure and blit the binary data onto that structure

Define a formatted structure in C# that has identical binary layout to the structure put to disk. This will use LayoutKind.Sequential and perhaps specify Pack = 1 if the Delphi structure is packed. You may need to use the MarshalAs attribute on some members to achieve binary equivalence. Then read the structure from disk into a byte array. Pin this array, and use Marshal.PtrToStructure on the pinned object address to deserialize. Now you have the data, you can write it how you please.

An example can be found here: Proper struct layout from delphi packed record

3. Read the structure field by field with a binary reader

Rather than declaring a binary compatible structure you can use a BinaryReader to read from a stream one field at a time. Method calls like Read, ReadInt32, ReadDouble, etc. let you work your way through the record. Remember that the fields will have been written in the order in which the Delphi record was declared. If the original record is aligned rather than packed you will need to step over any padding. Again, once you have the data available to your C# code you can write it as you please.

Sign up to request clarification or add additional context in comments.

1 Comment

If I were unfamiliar with Delphi, I would prefer solution 3. Probably a little slower, but for a one-time event, that wouldn't matter too much. But even then, I would look at my data with a hex editor, just to see if there are any filler bytes to be skipped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.