Hey Shabbir,
Very useful thread, this one. The thing is one becomes so proficient in programming & concerned with advanced implementation issues, that one often forgets small snippets & nuggets of code such as this.

So, Thx a lot for it.
Waise, I developed a Decimal Integer to Binary String or Hexa String Convertor. I have put the source code here ....
Ciao,
Rajiv
Code:
/* Decimal Integer to Binary String or Hexa String Convertor */
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULTVAL 0
char* dec_to_binstr(int,int=DEFAULTVAL);
char* dec_to_hexstr(int,int=DEFAULTVAL);
void main()
{
int choice;
unsigned int decqty,numbits;
do{
clrscr();
printf("1. DEC TO BIN\t2. DEC TO HEX\n3. EXIT");
printf("\nEnter choice : ");
scanf("%d",&choice);
if(choice==1 || choice==2)
{
printf("\n\n\nEnter the Decimal Quantity : ");
scanf("%d",&decqty);
printf("\nEnter a specific Number of Bits/Hexa-Digits if you want the O/P to contain a specific number of bits");
printf("\n(Enter '0' (or DEFAULTVAL) to leave blank so that program calculates required number of bits) : ");
scanf("%d",&numbits);
}
switch(choice)
{
default:
printf("\n\nINVALID CHOICE!! Please Try Again!!");
case 1:
printf("\n\nBinary Equivalent is : %s",dec_to_binstr(decqty,numbits));
break;
case 2:
printf("\n\nHexadecimal Equivalent is : %s",dec_to_hexstr(decqty,numbits));
break;
case 3:
break;
}
getch();
}while(choice!=3);
}
/* This Routine Converts a decimal Qty to binary Qty */
char* dec_to_binstr(int decnum,int numbits)
{
int i,j,k;
char *result;
char *temp;
printf("\n\n\nEntered in Routine : dec_to_binstr()");
printf("\ndecnum = %d\tnumbits = %d",decnum,numbits);
// Calculating Number of bits required
for(i=0;pow(2,i)<abs(decnum);i++);
printf("\nNo. of bits reqd (i) = %d",i);
// Checking Whether 'numbits' provided in Call is adequate
if(numbits!=DEFAULTVAL && numbits<i)
return "ERROR!! Incorrect 'numbits' Specification during Call!!";
// Allocating Memory for 'temp' & 'result'
else if(numbits==DEFAULTVAL)
// No 'numbits' provided in call
{
temp=(char*)malloc((i+1)*sizeof(char));
result=(char*)malloc((i+1)*sizeof(char));
}
else
// Function call has provided the 'numbits'
// Assuming that 'numbits' involves the 1 extra character slot required
// in case of signed integers
{
temp=(char*)malloc((numbits+1)*sizeof(char));
result=(char*)malloc((numbits+1)*sizeof(char));
}
// Performing Intermediate processing on 'temp'
for(j=0;decnum>0;j++)
{
temp[j]=char('0'+decnum%2);
decnum=decnum/2;
}
// Padding Any Bits Remaining if at all with Zeroes
for(;j<numbits;j++)
temp[j]='0';
temp[j]='\0';
// Forming the 'result' string by String Reversal
k=0;
if(numbits==DEFAULTVAL)
j=i-1;
else
j=numbits-1;
for(;j>=0;j--,k++)
result[k]=temp[j];
result[k]='\0';
return result;
}
char* dec_to_hexstr(int decnum,int numbits)
{
int i,j,k;
char *result;
char *temp;
int numtemp;
printf("\n\n\nEntered in Routine : dec_to_hexstr()!!");
printf("\ndecnum = %d\tnumbits = %d",decnum,numbits);
// Calculating Number of Hexa-digits required
for(i=0;pow(16,i)<abs(decnum);i++);
printf("\nNo. of Hexa-digits reqd (i) = %d",i);
// Checking Whether 'numbits' provided in Call is adequate
if(numbits!=DEFAULTVAL && numbits<i)
return "ERROR!! Incorrect 'numbits' Specification during Call!!";
// Allocating Memory for 'temp' & 'result'
else if(numbits==DEFAULTVAL)
// No 'numbits' provided in call
{
temp=(char*)malloc((i+1)*sizeof(char));
result=(char*)malloc((i+1)*sizeof(char));
}
else
// Function call has provided the 'numbits'
// Assuming that 'numbits' involves the 1 extra character slot required
// in case of signed integers
{
temp=(char*)malloc((numbits+1)*sizeof(char));
result=(char*)malloc((numbits+1)*sizeof(char));
}
// Performing Intermediate processing on 'temp'
for(j=0;decnum>0;j++)
{
numtemp=decnum%16;
if(numtemp>=10)
temp[j]=char('A'+(numtemp-10));
else
temp[j]=char('0'+numtemp);
decnum=decnum/16;
}
// Padding Any Bits Remaining if at all with Zeroes
for(;j<numbits;j++)
temp[j]='0';
temp[j]='\0';
// Forming the 'result' string by String Reversal
k=0;
if(numbits==DEFAULTVAL)
j=i-1;
else
j=numbits-1;
for(;j>=0;j--,k++)
result[k]=temp[j];
result[k]='\0';
return result;
}