Thursday, May 28, 2015

EBCDIC To ASCIIFile Converter Code C#


This code is written in C# to convert EBCDIC  to ASCII



using System;  
 using System.Collections.Generic;  
 using System.Text;  
 using System.IO;  
 using System.Text.RegularExpressions;  
 namespace ConsoleApplication1  
 {  
   class Program  
   {  
     static void Main()  
     {  
       StreamReader sr = new StreamReader("d:\\LAB\\CXAR2.block.txt");  
       StreamWriter sw = new StreamWriter("d:\\LAB\\CXAR2.DAT.ascii");  
       EBCDIC2ASCII obj = new EBCDIC2ASCII();  
       string line = "";  
       //char[] buffer=new char[256];  
       //byte[] bufferByte = new byte[256];   
       int i = 0;  
       while (!(sr.EndOfStream))  
       {  
         line = sr.ReadLine();  
         byte[] bufferByte = obj.EBCDIC_1(line);  
         char[] buffer = new char[bufferByte.Length - 1];  
         for (i = 0; i < bufferByte.Length - 1; i++)  
         {  
           buffer[i] = (char)bufferByte[i];  
         }  
         sw.WriteLine(buffer);  
       }  
       sr.Close();  
       sw.Close();  
     }  
   }  
   class EBCDIC2ASCII  
   {  
     public byte[] EBCDIC_1(string line)  
     {  
       string ebcdicTable = "@KMNPZ[\\]^_`akloz{|}ÀÁÂÃÄÅÆÇÈÉÑÒÓÔÕÖ×ØÙàâãäåæçèéðñòóôõö÷øùЁ‚ƒ„…†‡ˆ‰‘’“”•–—˜™¢£¤¥¦§¨©¬mjnLº»°~";  
       string asciiTable = " .(+&!$*); -/,%?:#@'\"{ABCDEFGHIJKLMNOPQR\\STUVWXYZ0123456789}abcdefghijklmnopqrstuvwxyzD_|><[]^=";  
       const byte space = (byte)32;  
       const byte tilde = (byte)126;  
       Regex rx = new Regex("");  
       string[] inBufChar = rx.Split(line);  
       List<char> lstInBuff = new List<char>();  
       List<byte> lstInBuff_Byte = new List<byte>();  
       char chrInBuff;  
       int i = 0;  
       for (i = 0; i < inBufChar.Length - 1; i++)  
       {  
         if (inBufChar[i] == "") { continue; }  
         chrInBuff =char.Parse(inBufChar[i]);  
         lstInBuff_Byte.Add((byte)chrInBuff);            
       }  
       //char[] inBufChar1 = new char[256];  
       byte[] inBuf = lstInBuff_Byte.ToArray();  
       byte[] outBuf = new byte[inBuf.Length-1];   
       for ( i = 0; i < line.Length-1; i++)  
       {  
         int inCharPos = ebcdicTable.IndexOf((char)inBuf[i]);  
         if (inCharPos == -1)  
         {  
           // Unknown EBCDIC character  
           outBuf[i] = space;  
         }  
         else  
         {  
           outBuf[i] = (byte)asciiTable[inCharPos];  
           if (outBuf[i] < space ||  
             outBuf[i] > tilde)  
           {  
             // Out of range ASCII character.  
             // Should never happen, as the translation table  
             // has no control or high-order characters on the  
             // output side ... but, just in case.  
             outBuf[i] = space;  
           }  
         }  
       }  
       return outBuf;  
     }  
     public byte[] EBCDICToASCIIFile(byte[] buffer,int recordLength)  
     {  
       byte[] outBuffer = BuildRecord(buffer, recordLength);  
       //fsOut.Write(outBuffer, 0, outBuffer.Length);           
       return outBuffer;  
     }  
     protected byte[] BuildRecord(byte[] buf, int record)  
     {  
       byte[] outBuf = new byte[256]; // Output record length  
       int outPos = 0;  
       outPos += CopyRange(buf, outBuf, 0, 12, outPos);              // Cust ID, district  
       if (outPos != outBuf.Length - 2)  
       {  
         //throw new ApplicationException(String.Format("BuildRecord(): Internal error at record {0}: {1} bytes have been written to output record, should be {2}.",  
         //record,  
         //outPos,  
         //outBuf.Length - 2));  
       }  
       // Add CR/LF  
       outBuf[outBuf.Length - 2] = (byte)13;  
       outBuf[outBuf.Length - 1] = (byte)10;  
       return outBuf;  
     }  
     protected int CopyRange(byte[] inBuf,  
          byte[] outBuf,  
          int start,  
          int length,  
          int outStart)  
     {  
       string ebcdicTable = "@KMNPZ[\\]^_`akloz{|}ÀÁÂÃÄÅÆÇÈÉÑÒÓÔÕÖ×ØÙàâãäåæçèéðñòóôõö÷øùЁ‚ƒ„…†‡ˆ‰‘’“”•–—˜™¢£¤¥¦§¨©¬mjnLº»°~";  
       string asciiTable = " .(+&!$*); -/,%?:#@'\"{ABCDEFGHIJKLMNOPQR\\STUVWXYZ0123456789}abcdefghijklmnopqrstuvwxyzD_|><[]^=";  
       const byte space = (byte)32;  
       const byte tilde = (byte)126;  
       for (int i = 0; i < length; i++)  
       {  
         int outPos = outStart + i;  
         int inCharPos = ebcdicTable.IndexOf((char)inBuf[start + i]);  
         if (inCharPos == -1)  
         {  
           // Unknown EBCDIC character  
           outBuf[outPos] = space;  
         }  
         else  
         {  
           outBuf[outPos] = (byte)asciiTable[inCharPos];  
           if (outBuf[outPos] < space ||  
             outBuf[outPos] > tilde)  
           {  
             // Out of range ASCII character.  
             // Should never happen, as the translation table  
             // has no control or high-order characters on the  
             // output side ... but, just in case.  
             outBuf[outPos] = space;  
           }  
         }  
       }  
       return length;  
     }  
   }  
 }  

No comments:

Post a Comment

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code