Extended Binary Coded Decimal Interchange Code (EBCDIC) is an 8-bit character encoding used mainly on IBM mainframe and IBM midrange computer operating systems.
EBCDIC descended from the code used with punched cards and the corresponding six bit binary-coded decimal code used with most of IBM's computer peripherals of the late 1950s and early 1960s. It is also employed on various non-IBM platforms such as Fujitsu-Siemens' BS2000/OSD, HP MPE/iX, and Unisys MCP.
Today I am going to discuss how to convert ASCII text into EBCDIC format. ASCII number 65 represents A where as it has different meaning in EBCDIC. Thats why you are reading this article. I will give you sample functions for converting ASCII into EBCDIC and EBCDIC into ASCII and conversion tables for easy use.
1) ASCII to EBCDIC
Most of the times these days, we are dealing with UNICODE characters and we need to convert them to ASCII 8-bit format unless you are working on legacy systems.
This function helps you converting given input string into ascii bytes.
private byte[] convertToASCIIcharacter(string input)
{
System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
byte[] character = ascii.GetBytes(input);
ASCII2EBCDIC_Char(ref character);
return character;
}
ASCII2EBCDIC_Char function looks like this.
private void ASCII2EBCDIC_Char(ref byte[] input)
{
int[] A2E = new int[128]{0,1,2,3,55,45,46,47,22,5,37,11,12,13,14,15,
16,17,18,19,60,61,50,38,24,25,63,39,28,29,30,31,
64,79,127,123,91,108,80,125,77,93,92,78,107,96,75,97,
240,241,242,243,244,245,246,247,248,249,122,94,76,126,110,111,
124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
215,216,217,226,227,228,229,230,231,232,233,74,224,90,95,109,
121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
151,152,153,162,163,164,165,166,167,168,169,192,106,208,161,7};
for (int counter = 0; counter < input.Length; counter++)
{
byte temp = (byte)A2E[input[counter]];
input[counter] = temp;
}
}
2) EBCDIC to ASCII
If you want to convert EBCDIC files to ASCII file format, you need to have conversion table. Here you go.
private void EBCDIC2ASCII(ref byte[] input)
{
int[] E2A=new int[256] {0,1,2,3,26,9,26,241,26,26,26,11,12,13,14,15,
16,17,18,19,26,26,8,26,24,25,26,26,28,29,30,
31,26,26,26,26,26,10,23,27,26,26,26,26,26,5,6,7,
26,26,22,26,26,26,26,4,26,26,26,26,20,21,26,26,
32,26,26,26,26,26,26,26,26,26,91,46,60,40,43,33,
38,26,26,26,26,26,26,26,26,26,93,36,42,41,59,94,
45,47,26,26,26,26,26,26,26,26,124,44,37,95,62,63,
26,26,26,26,26,26,26,26,26,96,58,35,64,39,61,34,
26,97,98,99,100,101,102,103,104,105,26,26,26,26,26,26,
26,106,107,108,109,110,111,112,113,114,26,26,26,26,26,26,
26,126,115,116,117,118,119,120,121,122,26,26,26,26,26,26,
26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,
123,65,66,67,68,69,70,71,72,73,26,26,26,26,26,26,
125,74,75,76,77,78,79,80,81,82,26,26,26,26,26,26,
92,26,83,84,85,86,87,88,89,90,26,26,26,26,26,26,
48,49,50,51,52,53,54,55,56,57,26,26,26,26,26,26};
for (int counter = 0; counter < input.Length; counter++)
{
input[counter] = (byte)E2A[input[counter]];
}
}
You can call the above function something like this
EBCDIC2ASCII(ref dump_record); and have dump_record declared as
byte[] dump_record
If you want to write as Binary file, open an instance of BinaryWriter class with encoding as Encoding.BigEndianUnicode.
Please leave your comments and start asking your programming questions on http://www.techielog.com
No comments:
Post a Comment