Sunday, July 12, 2015

Data Types and Operators in C#.NET

Data Types & Operators


Data Types
Data Type is a set of allowable values. It defines type of data and size of memory required. C#.net has following of categories of data types.

           Value Types
           Value Type store its contents in memory allocated in stack
           
           Reference Types
Reference Type stores the address of memory where data stored. Reference Type store the data in heap memory.

Pointer Types ( it is unsafe and not advised to use)
           
Value Types
SNO
Data Type
Size
Range
Default Value
Framework Type
1
Int
4 bytes
-2,147,483,648 to 2,147,483,647
0
System.Int32
2
Uint
4 bytes

0
System.UInt32
3
short
2 bytes
-32,768 to 32,767
0
System.Int16
4
ushort
2 bytes
0 to 65,535
0
System.UInt16
5
long
8bytes
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
0
System.Int64
6
ulong
8bytes
0 to 18,446,744,073,709,551,615
0
System.UInt64
7
byte
1 byte
0 to 255
0
System.Byte
8
sbyte
1 byte
-128 to 127

System.SByte
9
float
4 bytes

0.0F
System.Single
10
double
8 bytes

0.0
System.Double
11
decimal
16 Bytes

0
System.Decimal
12
bool
1 Byte

False

13
Struct




14
char
2 Bytes

‘\0’
System.Char
15
enum





Reference Types
  • class
  • interface
  • delegate
  • dynamic
  • string
  • object 

Pointer Types
            Pointer Type stores the address of another variable. Pointer Types can be used in unsafe context.
Ex;
            int* p1,p2;
int a=23;
            unsafe
{
            p1=&a;
}
Any of the following types may be a pointer type:
·         sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool
·         Any enum type.
·         Any pointer type.
·         Any user-defined struct type that contains fields of unmanaged types only.

To make run the unsafe code in c#.net, turn on ‘allow unsafe’ check box in visual studio 2012 under build properties (Project->Properties->Build).


Variable Declaration

Syntax:  <datatype> <variablename>;
int a =2;
int b=3,c=4;
float f=2.3F;   ( float f=2.3 will throw an error. In c# all decimal points will be treated as double. Hence float value has to be suffixed with F or f)
double b=2.45;
decimal c=2.6M;(decimal values should be suffixed with M or m)
string name=”sateesh”;




Operators
Arithmetic Operators
+, -, *, /
Assignment Operator
=
Arithmetic Assignment Operators
+=, -=, *=, /=
Relational Operators
>,<,>=,<=, !=, ==
Logical Operators
&& (and), || (OR), ! (NOT)
Ternary Operator
?:
Increment Operator
++
Decrement Operator
--

Examples:-
class Program
    {
        static void Main(string[] args)
        {
            int a = 3;
            int b = a ^ 2;
            int c = a + b;

            string s1 = "Jeorge";
            string s2 = "Bush";
            string s3 = s1 + " " + s2;

Console.Write("Values of variable: a={0},b={1},c={2},s1={3},s2={4},s3={5}",a,b,c,s1,s2,s3);
          
              a+=2; // this is equivalent to a=a+b;


        }
    }
Output : Values of variable: a=34,b=102,c=56,s1=Jeorge,s2=Bush,s3=Jeorge Bush

Note: + operator on strings concatenates the strings and on numeric data will add. + operator cannot be applied ob byte arguments.

Examples on Ternary Operator
int a=23;b=35
int big=a>b?a:b;

int a=34, b=102, c=56;
int big=a>b && a>c?a:(b>c)?b:c;

Examples on Increment and Decrement Operator
int a=105;
int b=a++;  (post increment)
value of b is 105 and value a is 106;
int a=105;
int b=++a;  (pre increment)
value of b is 106 and value a is 106;


Wednesday, July 8, 2015

How to encode large files into Base64 using c#?

Introduction
Base64 encoding is encoding binary data to string format by translating into using a radix-64 representation.
Contents
  1.  How to encode a string into Base64
  2.  How to encode large files into Base64 using c#


How to encode a string into Base64
Let us encode a string “ab” into base64
Sring
a
b
ASCII
91
92
Bit wise
0
1
0
1
1
0
1
1
0
1
0
1
1
1
0
0
Grouping Bits (6 bits into one group) 
6 bits
6bits
4bits
ASCII
22
53
12
Base64 Encode
W
1
M

Base64 Char set
Value
Char
Value
Char
Value
Char
Value
Char
Value
Char
Value
Char
0
A
11
L
22
W
33
h
44
s
55
3
1
B
12
M
23
X
34
i
45
t
56
4
2
C
13
N
24
Y
35
j
46
u
57
5
3
D
14
O
25
Z
36
k
47
v
58
6
4
E
15
P
26
a
37
l
48
w
59
7
5
F
16
Q
27
b
38
m
49
x
60
8
6
G
17
R
28
c
39
n
50
y
61
9
7
H
18
S
29
d
40
o
51
z
62
+
8
I
19
T
30
e
41
p
52
0
63
/
9
J
20
U
31
f
42
q
53
1


10
K
21
V
32
g
43
r
54
2






How to encode large files into Base64 using c#

.NET has a built-in function to convert a string into Base64 Encoded string. But it may throw an exception (like out of memory) when you are encoding huge data. I am going to create a function which can handle any size file in encoding data to Base64 encoded string
public class Base64Encoder
    {

        const int SegmentSize = 20 * 1024;
        public void Encode(string sourceFile, string destFile)
        {
/* Reading a file by segmentwise and encode into Base64string and then  write into file During Base64Encoding, 3 characters(3 bytes) will be converted into 4 characters(4 bytes). Hence we need to take segment of size multiples of 3
             */
            long actualSegmentSize = 3 * SegmentSize;
            long fileLength=0;
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
            {
                if (File.Exists(destFile)) File.Delete(destFile);
FileStream destStream = new FileStream(destFile, FileMode.Create, FileAccess.Write);
                long filePosition = 0;
                fileLength = sourceStream.Length;
                byte[] sourceSegment;
                byte[] base64EncodeSegment;
                while (filePosition < fileLength)
                {
                    if (filePosition + actualSegmentSize > fileLength)
                        sourceSegment = new byte[fileLength];
                    else
                        sourceSegment = new byte[actualSegmentSize];
                    sourceStream.Read(sourceSegment, 0, sourceSegment.Length);
base64EncodeSegment = System.Text.ASCIIEncoding.ASCII.GetBytes(Convert.ToBase64String(sourceSegment));
                    destStream.Write(base64EncodeSegment, 0, base64EncodeSegment.Length);
                    filePosition = sourceStream.Position;

                }

                destStream.Close();
               
            }
       
        }


        public void Decode(string sourceFile, string destFile)
        {


/* Reading Base64Encoded file by segmentwise and decode into string and then  write into file During Base64Encoding, 3 characters(3 bytes) will be converted into 4 characters(4 bytes). Hence we need to take segment of size multiples of 4 for decoding
             */
            long actualSegmentSize = 4 * SegmentSize;
            long fileLength = 0;
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
            {
                if (File.Exists(destFile)) File.Delete(destFile);
FileStream destStream = new FileStream(destFile, FileMode.Create, FileAccess.Write);
                long filePosition = 0;
                fileLength = sourceStream.Length;
                byte[] sourceSegment;
                byte[] base64EncodeSegment;
                while (filePosition < fileLength)
                {
                    if (filePosition + actualSegmentSize > fileLength)
                        sourceSegment = new byte[fileLength];
                    else
                        sourceSegment = new byte[actualSegmentSize];
                    sourceStream.Read(sourceSegment, 0, sourceSegment.Length);
base64EncodeSegment = Convert.FromBase64String(System.Text.ASCIIEncoding.ASCII.GetString(sourceSegment));
                    destStream.Write(base64EncodeSegment, 0, base64EncodeSegment.Length);
                    filePosition = sourceStream.Position;
                }

                destStream.Close();
            }
        }
    }