Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# encoding a file in EBCDIC with packed decimals

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.

like image 952
Matt Avatar asked May 16 '11 19:05

Matt


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

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.

like image 200
Jeffrey L Whitledge Avatar answered Oct 19 '22 20:10

Jeffrey L Whitledge


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.

like image 31
dan04 Avatar answered Oct 19 '22 20:10

dan04