Click to See Complete Forum and Search --> : byte mapped records - best approach?


lynda.com
April 2nd, 2009, 08:56 PM
I need to return a set of values that will be used in a mainframe application. I am using C#. I have a set of record definitions like this:

Header record:

TranCode - 2 bytes - ASCII 00

Filler - 1 byte ASCII SPACE (0x20)

TranName - 26 bytes - ASCII value - Left justified - Pad with ASCII 0x20

DataRecord:

DataTypeVal - 2 bytes - ASCII 00 thru 28

DataValue - 18 bytes - ASCII numeric values

RecordDelimiter - 4 bytes - ASCII "END."



Well, you get the idea. There will also be a trailing record to finish things up and the data records can be more than one. Ultimately this will be written to a file that is transmitted to the mainframe and then digested there.

Just to clarify, all of the data will be static except for data record elements (and their supporting records). The goal is to produce a sealed class library that the consumer can instantiate, then iterate through some DB records and calling an ADD method and finally an Execute method. The class library would then build the record data, write it to a file, zip it and then send it to the destination via secure FTP.

I need a reccomended approach to representing this data structure that is byte position specific in C#.

Framework is .NET 2

Thanks in advance!

lynda.com
April 3rd, 2009, 01:56 PM
Is this the wrong area to post this question?

tr00don
April 3rd, 2009, 02:45 PM
Before the C# pros answer your question, please let me make some comments and make some assuptions to see if I understand your question the way you meant it.

You say that "this will be written to a file that is transmitted to the mainframe", so you want to write some data to a binary file that has a header section/record, a data section including one or mode data records, and an end section (trailing record).

If my understanding is correct, then I think one approach would be to define 3 data structures: one for the header, one for a data unit, and one for the trailer; then, create an array of data units. When you instantiate a particular data structure, populate it first with the default values you indicated. There are many articles on the Web on writing/reading binary files in C# (for example: http://www.java2s.com/Code/CSharp/File-Stream/WorkingwiththeBinaryWriterClass.htm).

Mutant_Fruit
April 4th, 2009, 04:50 AM
What you probably want to do is separate the encoding/decoding from your actual datastructure. You can have a class internally which represents your data as:

TranName - 26 bytes - ASCII value - byte[] (or string?)
DataTypeVal - 2 bytes - ushort/short
DataValue - 18 bytes - byte[] (or string?)

Then your decoder will take care of validating and parsing the header/footer for you and the encoder will take care of adding a valid header/footer to the data before writing it to the database/file.