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;  
     }  
   }  
 }  

Thursday, May 7, 2015

DataBinding WPF


Here we look an example of simple data binding in WPF. In this example I use a class for Data Binding. Here we look at the program.

Step1: First we create a Grid in our project:
<Grid x:Name="StuInfo">
     <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" MinWidth="77"></ColumnDefinition>
          <ColumnDefinition></ColumnDefinition>
     </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
     </Grid.RowDefinitions>
</Grid>

Step2: After that we create two TextBlocks and Two TextBoxes in our program and we also create a Button (Next) to see the Next Record according to the program.
 
<TextBlock Text="First Name" Margin="10"></TextBlock>
<TextBlock Text="Last Name" Margin="10" Grid.Row="1"></TextBlock>
<TextBox Text="{Binding fname}" Margin="10" Grid.Column="1"></TextBox>
<TextBox Text="{Binding lname}" Margin="10" Grid.Column="1"  Grid.Row="1"></TextBox>
<Button HorizontalAlignment="Left" Margin="0,12,0,9" Name="button1" Width="75"Grid.Column="1" Grid.Row="2">Next</Button>

1.png

Step3: Now we add a Loaded Event handler in the .cs page. This event will be called when the client wants to load the application:
 
public partial class Window1 : Window
{
   
public Window1()
   {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Page_Loaded);
   }
   void Page_Loaded(object sender, RoutedEventArgs e)
   {

   }
}

Step4: Now we add a class to our program:
public class Stu{
    public string fname { getset; }
    public string lname { getset; }
}  


Step5: Now we write the following code in the Loded Event Handler:
 
void Page_Loaded(object sender, RoutedEventArgs e)
{
   Stu s = new Stu();
    {
      s.fname = "Viji";
      s.lname = "Karg";
    };
      this.StuInfo.DataContext = s;
}

As we mention Binding in TextBox in .xaml page :
 
<TextBox Text="{Binding fname}" Margin="10" Grid.Column="1"></TextBox>
<TextBox Text="{Binding lname}" Margin="10" Grid.Column="1"  Grid.Row="1"></TextBox>



Step6: Now we add another Event Handler, which is for the Next Button:
 
public Window1()
{
   InitializeComponent();
   this.Loaded += new RoutedEventHandler(Page_Loaded);
   this.button1.Click += new RoutedEventHandler(button1_Click);
}
void
 button1_Click(object sender, RoutedEventArgs e)
{
   Stu s = new Stu();
    {
       s.fname = "Sid";
       s.lname = "hari";
    };
   this.StuInfo.DataContext = s;
}

Here we add another data in our program. When we click on the Next Button, the output will be:

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