Sunday, July 22, 2012

16 Bit BIN - HEX Converter in C


#include
#include
void hex16(char bin[16]);

int main()
{
char bin[16];
char hex[4];
printf("Enter 16 digit binary number : ");
scanf("%s", &bin);
hex16(bin);
return 0;
}

void hex16(char bin[16])
{
int i,k,j,dec1;
char hex[4];
int dec[4];
dec1=0;
j=0;
//printf("Entered 16 digit number : %s", bin);
for(i=0;i<4;i++)
{
dec[dec1]=0;
for(k=0;k<4;k++)
{
printf("%c",bin[j]);
if(bin[j]=='1')
{
switch (k)
{
case 0:
dec[dec1]=dec[dec1] + 8;
break;
case 1:
dec[dec1]=dec[dec1] + 4;
break;
case 2:
dec[dec1]=dec[dec1] + 2;
break;
case 3:
dec[dec1]=dec[dec1] + 1;
break;
}
}
j++;
}
printf(" : %d", dec[dec1]);
switch(dec[dec1])
{
case 0:
hex[i]='0';
break;
case 1:
hex[i]='1';
break;
case 2:
hex[i]='2';
break;
case 3:
hex[i]='3';
break;
case 4:
hex[i]='4';
break;
case 5:
hex[i]='5';
break;
case 6:
hex[i]='6';
break;
case 7:
hex[i]='7';
break;
case 8:
hex[i]='8';
break;
case 9:
hex[i]='9';
break;
case 10:
hex[i]='A';
break;
case 11:
hex[i]='B';
break;
case 12:
hex[i]='C';
break;
case 13:
hex[i]='D';
break;
case 14:
hex[i]='E';
break;
case 15:
hex[i]='F';
break;
}
printf("\n");
dec1++;
}
printf("Final Hex : %s", hex);
}

Output

Enter 16 digit binary number : 1100111100001010
1100 : 12
1111 : 15
0000 : 0
1010 : 10
Final Hex : CF0A

Note - Code is written in DevC++5.2.

Monday, April 11, 2011

A sea shore of Alibaug (Mandava Beach)

हा सागरी किनारा











Monday, April 4, 2011

JDBC Sample Code, Using DSN & MS-Access

import java.sql.*;
import java.io.*;

class Database
{
Connection con;
ResultSet rs;
Statement stmt;
BufferedReader br;
String str,temp;
Database()
{
try
{
con=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:ACCESS_DB");
stmt=con.createStatement();
str=new String();
temp=new String();
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Connection Ok... Tested...");
}
catch(Exception E)
{
System.out.println("Connection Error : " + E.getMessage());
}
}
public void search()
{
System.out.println();
try
{
System.out.print("Enter string to be search : ");
str=br.readLine();
temp="Select * from Mobile Where Name Like '%" + str + "%' OR Num Like '%" + str + "%'";
System.out.println(temp);
rs=stmt.executeQuery(temp);
while(rs.next())
{
for(int i=1;i<=2;i++)
{
System.out.print(rs.getObject(i)+"\t");
}
System.out.println();
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void add_new()
{
System.out.println();
String no,name;
try
{
System.out.print("Enter Mobile No : ");
no=br.readLine();
System.out.print("Enter Name : ");
name=br.readLine();
temp="Insert into Mobile Values('"+no+"','"+name+"')";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Record Successfully Inserted...");
}
else
{
System.out.println("Record Insertion failure...");
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void select_all()
{
System.out.println();
try
{
rs=stmt.executeQuery("Select * from Mobile");
while(rs.next())
{
for(int i=1;i<=2;i++)
{
System.out.print(rs.getObject(i)+"\t");
}
System.out.println();
}
}
catch(Exception E)
{
System.out.println("SQL Error : " + E.getMessage());
}
}
public void con_close()
{
try
{
con.close();
}
catch(Exception E)
{
System.out.println("Connection Closing Error : " + E.getMessage());
}
}
public void update_record()
{
System.out.println("1.Search by Name");
System.out.println("1.Search by Number");
System.out.print("Enter your choice : ");
int ch1;
try
{
ch1=Integer.parseInt(br.readLine());
switch(ch1)
{
case 1:
System.out.print("Enter Name : ");
str=br.readLine();
System.out.print("Enter New Number : ");
temp="Update Mobile Set Num='"+br.readLine()+"' Where Name='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Updation Successful...");
}
else
{
System.out.println("Updation Unsuccessful...");
}
break;

case 2:
System.out.print("Enter Number : ");
str=br.readLine();
System.out.println("Enter New Name : ");
temp="Update Mobile Set Name='"+br.readLine()+"' Where Num='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Updation Successful...");
}
else
{
System.out.println("Updation Unsuccessful...");
}
break;

default:
System.out.println("Wrong Choice... Try Again...");
}
}
catch(Exception E)
{
System.out.println("Updation Error : "+E.getMessage());
}
}
public void delete_record()
{
System.out.println("1. Search by Name");
System.out.println("2. Search by Number");
System.out.print("Enter your choice : ");
int ch1;
try
{
ch1=Integer.parseInt(br.readLine());
switch(ch1)
{
case 1:
System.out.print("Enter Name to be deleted : ");
str=br.readLine();
temp="Delete From Mobile where Name='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Deletion Successful...");
}
else
{
System.out.println("Deletion failure...");
}
break;

case 2:
System.out.print("Enter Number to be deleted : ");
str=br.readLine();
temp="Delete From Mobile where Num='"+str+"'";
System.out.println(temp);
if(!stmt.execute(temp))
{
System.out.println("Deletion Successful...");
}
else
{
System.out.println("Deletion failure...");
}
break;

default :
System.out.println("Wrong Choice...");
}
}
catch(Exception E)
{
System.out.println("Record Delection Error : " + E.getMessage());
}
}
}

public class DB_SAMPLE
{
public static void main(String args[])
{
Database db=new Database();
BufferedReader br;
int ch=0;
System.out.println("");
System.out.println("Mobile Contact Database for Personal Desktop Use");
System.out.println("");
System.out.println("");
System.out.println("1. See Entire Database");
System.out.println("2. Search");
System.out.println("3. Add new Record");
System.out.println("4. Delete");
System.out.println("5. Update");
System.out.print("Enter your Choice : ");
try
{
br=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br.readLine());
}
catch(Exception E)
{
System.out.println("Console Reading Error : " + E.getMessage());
}

switch (ch)
{
case 1:
db.select_all();
break;

case 2:
db.search();
break;

case 3:
db.add_new();
break;

case 4:
db.delete_record();
break;

case 5:
db.update_record();
break;

default:
System.out.println("Wrong Choice... Try Again...");
}
db.con_close();
}
}

Friday, March 18, 2011

C++ problem

Hello Everybody I tried this code. Code gives 0 warning and 0 errors. I tested under Turbo C++ environment. My Question is How Constructor of all classes is called without creating an object? I know once constructor of class C is called it is automatically called upper class’s constructor. Basic question is how Constructor of class C is called with creating an object of that class?

**********CODE**********

#include<iostreams.h>

#include<conio.h>

#include<stdio.h>


class A

{

public :

A()

{

cout<<"A\n";

}

};


class B : public A

{

public :

B()

{

cout<<"B\n";

}

};


class C : public B

{

public :

C()

{

cout<<"C\n";

}

};


void main()

{

clrscr();

C();

getch();

}



**********OUTPUT**********


A

B

C