Number Conversions

Convert an integer number into binary number

 int inttobin(unsigned int a,int *bin){
int one=0;
unsigned int c=1;
int i ;
for(i=31;i>=0;i--){
one = one + (a&c);
bin[i] = (a&c)?1:0;
c<<=1;}
return one; }

Convert a binary number into binary number

 unsigned int creatnum(int *num){
int i;
unsigned int a=0;
for(i=0;i<32;i++){
a= a|num[i];
if(i!=31)
a <<= 1;}
return a;}

Integer <-> Hex <-> Integer

char hex[100];
int num = 4095;
sprintf(hex,”%X”,num);// convert the num in upper case hex decimal in hex
sprintf(hex,”%x”,num);// convert the num in lower case hex decimal in hex
sscanf(hex,”%x”,&num);// convert the hex number hex in integer num

convert any integer number on any base from 2 to 36

#include<string.h>
#include<stdio.h>
#define MAXDGT 200 /* maximum number of digit */
/* ********************************************************* */
/* A general swap function that swap two object. */
/* ********************************************************* */
template <class T>
void swap(T &x, T &y)
{ T tmp=x; x=y; y=tmp; };
/*************************************************************** */
/* A general string reverse function that reverse the */
/* passed string and store the string int to the parameter. */ 
/*************************************************************** */
void revstr(char *str)
{ int i=0,l=strlen(str);
 while(i<l)
 swap(str[i++],str[--l]);
}
/******************************************************************* */
/* A general base conversion function */
/* ******************************************************************* */
char *itob(long int n,int b=10)
{ char num[MAXDGT];
 int j,sign;
 register int i=0;
 if( (sign=n) <0 )
 n= -n;
do { j=n%b;
 num[i++]=(j<10) ? (j+'0'): ('A'+j-10);
 }while((n/=b)!=0);
 if(sign < 0) num[i++]='-';
 num[i]='';
 revstr(num);
 return num;
}
int main(void)
{ printf(itob(71],36)); }

Decimal To Roman

For example: i, v, x, l, c, m for 1, 5, 10, 50, 100 and 500 respectively

/* ********************************** */
/* Convert number 1 to 10 */
/* ********************************** */
void unit(int n){ 
switch(n){
 case 3 : printf("i");
 case 2 : printf("i");
 case 1 : printf("i"); break;
 case 4 : printf("i");
 case 5 : printf("v"); break;
 case 6 : printf("vi"); break;
 case 7 : printf("vii"); break;
 case 8 : printf("viii"); break;
 case 9 : printf("ix"); break;}
}
/* ********************************** */
/* Convert number 10 to 100 */
/* ********************************** */
void ten(int n){ 
switch(n){
 case 3 : printf("x");
 case 2 : printf("x");
 case 1 : printf("x"); break;
 case 4 : printf("x");
 case 5 : printf("l"); break;
 case 6 : printf("lx"); break;
 case 7 : printf("lxx"); break;
 case 8 : printf("lxxx"); break;
 case 9 : printf("xc"); break;}
}
/* ********************************** */
/* Convert number 100 to 500 */
/* ********************************** */
void hnd(int n){ 
switch(n){
 case 3 : printf("c");
 case 2 : printf("c");
 case 1 : printf("c"); break;
 case 4 : printf("c");
 case 5 : printf("M"); break;}
}
/* ************************************************* */
/* Convert an integer number into roman system */
/* ************************************************* */
void roman(int n){ 
int a,i;
 if(n>=500)
 { a=n/500 ;
 for(i=1;i<=a;i++)
 printf("M");
 }
 n=n%500;
 hnd(n/100);
 n=n%100;
 ten(n/10);
 unit(n%10);
} main(void)
{
roman(390);
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s