C# encoding a file in EBCDIC with packed decimals
Asked Answered
S

2

8

I have to prepare a file for an outside entity, and this entity uses an IBM mainframe with COBOL and requires the file to be encoded in EBCDIC. I am creating the file in C#. I guess this is a two-part question...firstly, is the code below sufficient to ensure that the file will be generated in a manner that they can read?

StreamWriter sw = new StreamWriter(@"C:\EBCDICFileEquivalent.txt", false, Encoding.GetEncoding(37));

Secondly, several fields in the file require packed decimal, in the format S(9)13 COMP-3. Since I have already specified EBCDIC encoding, can I just write the decimal number (e.g. 1234500000001) to the file using:

sw.WriteLine("1234500000001C"); 

...and have it turn out correctly in the generated file?

Have never done anything with EBCDIC or COMP-3.

Segalman answered 16/5, 2011 at 19:47 Comment(0)
B
2

That code should create an EBCDIC file just fine. This usually isn't necessary, since the FTP process will usually convert the file automatically from ASCII to EBCDIC, but if that is not the case, then the file you are creating should be correct and may be transmitted as binary rather than text.

Writing the number out as text with the appropriate substitutions to satisfy the mainframe zoned signed decimal format is the correct procedure. Of course, making these substituions is a pain, but writing them out as a string as you are doing is the right way to get it into the file that the mainframe can read.

Buhrstone answered 16/5, 2011 at 20:11 Comment(3)
The FTP part is very important. If you do the ASCII to EBCDIC conversion on your own you must make sure that you transfer using binary mode, otherwise you run the risk of double-converting the file.Shroyer
Yeah, you should probably just create an ASCII file on the first attempt, and let the system do the conversions. Only if that doesn't work should you mess around with making an EBCDIC file.Buhrstone
@dan04 - My answer is not wrong. The OP specified both EBCDIC and COMP-3, which are not compatible formats. My answer is correct for zoned decimals in EBCDIC. I trust that your answer is correct for COMP-3 (which I have not had experience with). Which of our answers is applicable depends on what the actual requirements are, which is not clear from the question.Buhrstone
E
1

Secondly, several fields in the file require packed decimal, in the format S(9)13 COMP-3. Since I have already specified EBCDIC encoding, can I just write the decimal number (e.g. 1234500000001) to the file using:

sw.WriteLine("1234500000001C");

...and have it turn out correctly in the generated file?

No, you can't. That statement would write the EBCDIC bytes:

F1 F2 F3 F4 F5 F0 F0 F0 F0 F0 F0 F0 F1 C3

But this field is COMP-3. That's not encoded characters; it's BCD with a sign nybble (C=positive, D=negative, F=unsigned).

12 34 50 00 00 00 1C

Assuming code page 37 is correct, you could write this to the file using the string "\u0012\u0094&\u0000\u0000\u001C", but it would make much more sense to write these non-text fields in binary mode.

Errolerroll answered 17/5, 2011 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.