Archive

Archive for February, 2007

Recoverable Data Storage

February 27th, 2007

Ever felt helpless at the hands of the Cyclic Redundancy Check (CRC) error? Here’s what you should do to keep it at bay.

A cyclic redundancy check (CRC) is a type of hash function used to produce a checksum – a small, fixed number of bits – against a block of data, such as a packet of network traffic or a block of a computer file.

Source: Wikipedia

Storing files in the form of compressed RAR format has benefits beyond just solid compression. Most notably, the ability to split archives and recover data from corrupted ones. WinRAR is a lightweight, easy to use and powerful RAR archiver. It can be obtained from Rar Lab. While creating compressed archives, select the put recovery record option.

Put Recovery Record

Putting the recovery record allows the recovery of data from damaged archives in most of the cases. The overhead data added is very less, generally 1 – 2 percent. Despite the extremely small size, the reliability it adds is really amazing. Around 75% of damaged archives are recoverable.

Data stored in removable media such as CDs become corrupt with wear & tear. Sometimes, in spite of imparting great care, the files may become unreadable. The problem is heightened when the size of the files are quite large, as a few unreadable sectors would render the file unable to be copied. Thankfully, there are softwares to recover such files. CDCheck is one such handy tool. It extracts whatever amount of data that can be read and pads up the ones which can’t. Thus allowing even files with CRC errors to be saved locally.

Using CDCheck and WinRAR (with ‘Put Recovery Record’) in conjugation is a highly safe way of backing up data.

Tips

C# Tutorial : Lesson 01 – Hello World Program

February 14th, 2007

This is the first installment of my venture to explain the bits and pieces of C# that I have managed to learn at NIIT. In this lesson we would be creating the Hello World application.

First Step: Create a New Console Application in Visual C#

The IDE automatically adds the following code:-

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HelloWorld
{
	class Program
	{
		static void Main(string[] args)
		{
		}
	}
}

Lets take a look at the first 3 lines. All of these lines begin with the keyword using. A namespace is an abstract collection of classes. The .NET Framework has a huge set of classes. They are grouped under namespaces for easy accessibility, better categorization and to prevent name clashing. For Example, the System.IO namespace contains all the classes, functions & methods required for Input Output operations, thus making life easier for us human beings. While the namespaces have remarkable benefits, they tend to bring in some potential inconveniences too. Take the System.Console.WriteLine() method for example. The method has to be called along with its complete path in the hierarchy, a really tiresome process to do repeatedly. This is when the using keyword comes into aid by importing the namespaces. When imported, the classes inside the namespace can be accessed just by their names. So, once we have imported the System namespace, the above method can be called as Console.WriteLine().

Any .NET application that we make, has to reside in a namespace of its own, so we write the statement namespace HelloWorld. All the classes you create under this namespace can be accessed as NameSpaceName.ClassName.

Next we have the class declaration itself. Inside you’ll find the static void Main function. This is the entry point for the application. Think of it as the outer door of your house. Any one coming inside has to first pass through it. The entry point must be declared as static because that way the main function can be invoked even without creating an object of the Program class. Also the name of the entry function should be Main, the capital M counts too.

Any application can take parameters, when run from the command prompt. Say you ran the Notepad application as follows Notepad.exe MyFile.txt. The text MyFile.txt is the parameter passed to it, Notepad reads this parameter and opens the file. Similarly, we may need to work with arguments passed to our application. This is why we use the string[] args inside the Main function.

Up until this point I have described the auto generated code. Now let us type in some code of our own. Keeping with the tradition of the Hello World program we type in Console.WriteLine(“Hello World”); inside the body of the Main function. The Console class resides in the System namespace and represents the standard input, output and error streams for console application. The WriteLine method displays the supplied argument(s) on the console.

Press F5 to run the application. You’ll see a console window with the text Hello World appear. Don’t worry if the window closes on its own. Its because the application reaches the end point and exits. You can add the following line to make it halt for an input before exiting: Console.ReadLine();

This is how our final code looks like:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HelloWorld
{
	class Program
	{
		static void Main(string] args)
		{
			Console.WriteLine("Hello World");
			Console.ReadLine();
		}
	}
}
Escape Sequences

Much like C/C++ , C# has a list of escape sequences. Escape Sequences or Escape Characters are some special characters which cannot be displayed directly. For Example, the newline character or the Double Quotes.

White space being ignored by C#, the newline escape sequence is the only way to display the newline character in the output. The double quotes are used to enclose strings hence they cannot be used directly. To display the special characters such as these, we use escape sequences. All escape sequences begin with the backslash character \

Example Usage:-

Console.WriteLine("This is a \n multi-line text");

Output:-
This is a
multi-line text

Console.WriteLine("C# is a \"Programming Language\"");

Output:-
C# is a “Programming Language”

Here’s the list of the escape characters in C#

Escape Sequence Equivalent Character
\’ Single Quotation
\” Double Quotation
\\ Backslash
\0 NULL
\a Alert
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab

Note: Equivalent for Visual Basic’s vbCrLf is \r\n

C#

Non Repeating – Random Number Generation Class

February 8th, 2007

Sometimes, we require to generate non-repeating random numbers. Here’s the code I wrote to do just that. The numbers to be generated can be within the specified range or passed as a param array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Public Class RandomNumber
    Dim NumDomain As New ArrayList      ' The Numbers to be generated
    Dim Nums As New ArrayList           ' The Numbers waiting to be generated
 
    Sub New(ByVal Start As Integer, ByVal Finish As Integer)
        ' Sequential Numbers
        Dim I As Integer
        For I = Start To Finish
            NumDomain.Add(I)
        Next
 
        AddItems()
    End Sub
 
    Sub New(ByVal ParamArray Numbers() As Integer)
        ' Numbers in a Param Array
        Dim Number As Integer
        For Each Number In Numbers
            NumDomain.Add(Number)
        Next
 
        AddItems()
    End Sub
 
    Private Sub AddItems()
 
        Nums.Clear()
        ' Insert All Numbers Into the Array
        Dim I As Integer
        For I = 0 To NumDomain.Count - 1
            Nums.Add(NumDomain(I))
        Next
    End Sub
 
    Public Function GetRandomNumber() As Integer
        If Nums.Count = 0 Then
            ' Re-add the Items, when all the numbers have been generated.
            AddItems()
        End If
 
        ' Return a Random Item and Remove it from the List
        Dim Ch As Integer
 
        ' Using Timer as the seed for the Random Number Generation
        Randomize(Microsoft.VisualBasic.Timer)
 
        ' Random Number is generated within the range.
        Ch = 0 + CInt(Rnd() * (Nums.Count - 1))
 
        Dim Num As Integer = Nums(Ch)
        Nums.RemoveAt(Ch)
        Return Num
    End Function
End Class
Mechanism

The overloaded constructors allow numbers to be generated either within a range or from the numbers passed as parameters. Internally there are two array lists: NumDomain & Num. The former contains all the numbers within the domain and the latter holds the numbers that have not been generated. The GetRandomNumber function returns a random number from the Num array list and also removes it from there. When all the numbers have been poped (not to be confused with stack poping) out, the Num array list is re-filled with the values in NumDomain and the cycle continues. Thus, allowing no repeats per cycle.

Visual Basic

Notepad++

February 8th, 2007

Notepad++Notepad++ is a free, light weight but powerful editor which supports several programming languages, running under the MS Windows environment. With features such as syntax highlighting and folding, full drag & drop support, auto completion, regular expression search/replacement this tool is a must have. It can also be set as the default HTML editor, so when you click on View > Source under Internet Explorer, the HTML code will open with Notepad++. Another plus point is that it has a plugin architecture which allows further extendability. One of the bundled plugins is a Hex Editor.

This project, based on the Scintilla edit component (a very powerful editor component), written in C++ with pure Win32 API and STL (that ensures the higher execution speed and smaller size of the program), is under the GPL Licence.

Quick Links:-

Freeware

The Essence of Visual Basic

February 7th, 2007

Simplicity and ease of use, is what makes Visual Basic the most popular programming language in the World. Introduced as the Rapid Application Development (RAD) Tool, Visual Basic gained much fame because of the innovative features it pioneered:-

The name visual itself is very important, for the first time, people could drag and drop controls onto a design surface and see how a program would look without going through the lengthy edit-compile-test cycles required by other languages.

Stepping Through Code

Stepping through code while debugging – just ask any VB developer if they could do without it.

Stepping Through

The introduction of Active X Controls took the idea of code re-usability to a completely new level. VB was also the first popular general-purpose language to offer truly integrated database access. The IDE concept that VB revolutionized has now been incorporated by non Microsoft products too (Delphi, Java).

Intellisense

It speeds up software development by reducing the amount of keyboard input required and allows less reference to external documentation due to embedded function signatures and short descriptions.

Intellisense

Case Insensitivity

Nothing personal against the shift key, but holding it up and down is really annoying. Try typing miCRoSCoPiC^eaRthLinG for a change.

Shift Key

True it lacked the pure object oriented power of other languages, but hasn’t it caught up with the new .NET version? As we programmers grow lazier day by day, changes such as these are immensely welcome and with the further advent of hi-tech sidekicks, we will do so even more.

General

The .NET Framework & The CLR

February 6th, 2007

Microsoft .NET Framework
The .NET Framework is Microsoft’s answer to the JAVA platform. With heavy investments going into the development, one can’t question the push Microsoft is giving to this technology. Living in the hi-tech industry and not interacting with the .NET Framework is highly unlikely, considering the market share that Microsoft’s Operating Systems have. I am writing this article as a means to further the knowledge that I would be gaining while learning the MCAD Training Kit (maybe in the process, helping out some beginners). For those of you who don’t know what the MCAD is, its one of the certification program offered by Microsoft. Short for Microsoft Certified Application Developer, the MCAD is aimed at developers. The MCAD I would be trying, is for the .NET Framework. For further information on Microsoft’s certification programs, check out the following links:-

The .NET Framework

Basically the .NET Framework is comprised of two components: The CLR and the .NET Class Library. CLR stands for Common Language Runtime and allows the interoperability between the various .NET languages. The class library is comprised of a lot of reusable components (Classes, Functions, Methods etc) that aid us in doing common repetitive operations.

The CLR

The CLR sweeps away the traditional concept of the Application having to manage everything from Memory Allocation & Garbage Collection to Thread Management. The .NET Framework is responsible for performing the above mentioned operations. Its another excellent move as developers can now focus on matters concerning the application’s logic without having to waste time on managing these iterative issues.

What’s with the multiple programming languages?

Developers using Microsoft Application Development Tools, could be natively put into two categories – those from the C/C++ background and those with the history of the BASIC language. Packed with a lot of punch & power, C/C++ was the choice for making advanced programs. But it had its shortcomings too, most noticeably the complexities involved with the language itself. Case Sensitivity and type strictness are a few of them to be named. While these measures minimize the potentially dangerous bugs that would take us hours to determine and debug, they are annoying to some developers. BASIC was aimed at being a friendly, English like language that could be understood and mastered by the average human. Originally it was way behind its counterpart in terms of power, but with time it has been catching up.

With introduction of the CLR based .NET Framework, Microsoft has instilled the freedom of language to the developers. Developers with expertise in different .NET languages can now work together in projects, thus increasing productivity.

The .NET Languages

Apart from those offered by Microsoft – VB, C# & C++ there are other languages that target the .NET Framework. COBOL, PERL, FORTRAN are just a few to name. All .NET Languages follow the Common Language Specification (CLS) to facilitate this.

How does the Interoperability work?

Programs written in .NET Languages are not compiled to machine code, rather into an Intermediate Language known as MSIL (Microsoft Intermediate Language) or just IL. So, any assembly written in the .NET languages, are the same internally. This implies that the atomic data types such as Integer, Float, etc are now the same in VB, C# & C++.

Because of being compiled to MSIL, the reverse engineering of code becomes easier for .NET assemblies. It can seriously compromise security of a program, but thankfully there are obfuscation techniques to overcome it. Microsoft Visual Studio 2005 ships with a lite edition of dotfuscator. There are various other Obfuscators available in the market too.

Basic Structure of a .NET Application

.NET applications are comprised of assemblies. An assembly is a self-descriptive collection of codes, resources and metadata. Each assembly has a manifest which provides information pertaining to the assembly viz Assembly naming & versioning, list of all types exposed by the assembly, dependency on other assemblies & code security instructions. The manifest can exist externally or internally within a module of the assembly.

Compilation & Execution

As mentioned before, the .NET applications are compiled to the MSIL. During runtime, the MSIL is compiled into native binary code by the Just In Time (JIT) compiler. When the execution begins, the starting assembly is loaded and its manifest is validated. After all the security requirements are met, the assembly is executed. An important feature of the JIT compiler is that only the execution path of the assembly is compiled, thus maximizing performance.

General

The .NET Framework & The CLR

February 6th, 2007


The .NET Framework is Microsoft’s answer to the JAVA platform. With heavy investments going into the development, one can’t question the push Microsoft is giving to this technology. Living in the hi-tech industry and not interacting with the .NET Framework is highly unlikely, considering the market share that Microsoft’s Operating Systems have. I am writing this article as a means to further the knowledge that I would be gaining while learning the MCAD Training Kit (maybe in the process, helping out some beginners). For those of you who don’t know what the MCAD is, its one of the certification program offered by Microsoft. Short for Microsoft Certified Application Developer, the MCAD is aimed at developers. The MCAD I would be trying, is for the .NET Framework. For further information on Microsoft’s certification programs, check out the following links:-

The .NET Framework

Basically the .NET Framework is comprised of two components: The CLR and the .NET Class Library. CLR stands for Common Language Runtime and allows the interoperability between the various .NET languages. The class library is comprised of a lot of reusable components (Classes, Functions, Methods etc) that aid us in doing common repetitive operations.

The CLR

The CLR sweeps away the traditional concept of the Application having to manage everything from Memory Allocation & Garbage Collection to Thread Management. The .NET Framework is responsible for performing the above mentioned operations. Its another excellent move as developers can now focus on matters concerning the application’s logic without having to waste time on managing these iterative issues.

What’s with the multiple programming languages?

Developers using Microsoft Application Development Tools, could be natively put into two categories – those from the C/C++ background and those with the history of the BASIC language. Packed with a lot of punch & power, C/C++ was the choice for making advanced programs. But it had its shortcomings too, most noticeably the complexities involved with the language itself. Case Sensitivity and type strictness are a few of them to be named. While these measures minimize the potentially dangerous bugs that would take us hours to determine and debug, they are annoying to some developers. BASIC was aimed at being a friendly, English like language that could be understood and mastered by the average human. Originally it was way behind its counterpart in terms of power, but with time it has been catching up.

With introduction of the CLR based .NET Framework, Microsoft has instilled the freedom of language to the developers. Developers with expertise in different .NET languages can now work together in projects, thus increasing productivity.

The .NET Languages

Apart from those offered by Microsoft – VB, C# & C++ there are other languages that target the .NET Framework. COBOL, PERL, FORTRAN are just a few to name. All .NET Languages follow the Common Language Specification (CLS) to facilitate this.

How does the Interoperability work?

Programs written in .NET Languages are not compiled to machine code, rather into an Intermediate Language known as MSIL (Microsoft Intermediate Language) or just IL. So, any assembly written in the .NET languages, are the same internally. This implies that the atomic data types such as Integer, Float, etc are now the same in VB, C# & C++.

Because of being compiled to MSIL, the reverse engineering of code becomes easier for .NET assemblies. It can seriously compromise security of a program, but thankfully there are obfuscation techniques to overcome it. Microsoft Visual Studio 2005 ships with a lite edition of dotfuscator. There are various other Obfuscators available in the market too.

Basic Structure of a .NET Application

.NET applications are comprised of assemblies. An assembly is a self-descriptive collection of codes, resources and metadata. Each assembly has a manifest which provides information pertaining to the assembly viz Assembly naming & versioning, list of all types exposed by the assembly, dependency on other assemblies & code security instructions. The manifest can exist externally or internally within a module of the assembly.

Compilation & Execution

As mentioned before, the .NET applications are compiled to the MSIL. During runtime, the MSIL is compiled into native binary code by the Just In Time (JIT) compiler. When the execution begins, the starting assembly is loaded and its manifest is validated. After all the security requirements are met, the assembly is executed. An important feature of the JIT compiler is that only the execution path of the assembly is compiled, thus maximizing performance.

General