Chittagong University of Enginnering and Technology, Bangladesh

ACMBEGINNER

 

Home\ Tutorial \ Number Base Conversion

 Version 2.0

 Number Base Conversion    
Integer to Binary 

There are some mathematical basic and equations to convert an integer to binary number. But in computer with C/C++ we need not to know all of that. As we know computer represent any data in its memory with binary system. C/C++ can directly use that without doing any mathematical conversion. But for doing this, you need the knowledge about the bit-wise-operator, the great tools of C/C++. Using the following code you can convert a number into binary system and stores the binary number into an array. It also return the number of 1 in the binary representation.

 
     /*	nbconversion.cpp

	Implementation of various number base conversion
	Compiled : CYGNU & Microsoft Visual C++ 6.0
	Copyright 2003 by M H Rasel & CUET Old Sailor ; all rights reserved. 
	Permission is granted for use in non-commercial applications provided this 
	copyright notice remains intact   and unchanged.
*/

/* ********************************************************* */
/*     Convert an integer number into binary number	     */
/*     Input  : An integer number and an array		     */
/*     Return : Number of one (1) int the representation     */
/* ********************************************************* */
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; 
}

Binary to Integer

There are also some mathematical basic and equations to convert a binary number into decimal number. But in computer with C/C++ we need not to know all of that in the same way. Using the following code you can convert a binary number into integer.

/*	nbconversion.cpp
	Implementation of various number base conversion
	Compiled : CYGNU & Microsoft Visual C++ 6.0
	Copyright 2003 by M H Rasel & CUET Old Sailor ; all rights reserved. 
   	Permission is granted for use in non-commercial applications provided
   	this copyright notice remains intact   and unchanged.
*/

/* *********************************************************	*/
/*     Convert a binary number into binary number	 	*/
/*     Input  : An array that representation an integer	        */
/*     Return : The integer 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

Thought you have to know something about the bit-wise operator to convert numbers between binary and integer but to convert a number into hex or from hex to integer is very simple. You have to know just one function to do this.

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 

Integer to any base
To convert a integer number into a hex number or binary number you do not need any mathematical knowledge but to convert a integer number into any base you need the knowledge. And a programmer this knowledge is very important for you. However the following code help you to convert any integer number on any base from 2 to 36.

/*	nbconversion.cpp
	Implementation of various number base conversion
	Compiled : CYGNU & Microsoft Visual C++ 6.0
	Copyright 2003 by M H Rasel & CUET Old Sailor ; all rights reserved. 
	Permission is granted for use in non-commercial applications provided 
 	this copyright notice remains intact and unchanged.
*/
#include<string.h> 
#include<stdio.h> 
  
#define MAXDGT 200  	/* maximum number of digit */ 
  
/* *********************************************************	*/
/*     A general swap function that swap two object.	 	*/
/*     Input  : Two object.					*/
/*     Return : None						*/
/* *********************************************************	*/
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. 	*/
/*     Input  : A string					   	*/
/*     Return : None							*/
/* *****************************************************************	*/
void revstr(char *str) 
{	int i=0,l=strlen(str); 
	while(i<l) 
		swap(str[i++],str[--l]); 
} 
 
/* ******************************************************************* */
/*     A general base conversion function 			       */
/*     Input  : A number n and a base b			   	       */
/*     Return : A character array which contain the number n in base b */
/* ******************************************************************* */
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]='\0'; 
	revstr(num); 
	return num; 
} 
  
/* Sample main */ 
main(void) 
{	printf(itob(71],36));	} 
Decimal To Roman

Roman number is very useful number system. Often we need to convert a decimal number into Roman number. In Roman number the following symbol are used. 

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


The following code help you out to convert a number into its decimal number.

    /*	nbconversion.cpp
	Implementation of various number base conversion
	Compiled : CYGNU & Microsoft Visual C++ 6.0
	Copyright 2003 by M H Rasel & CUET Old Sailor ; all rights reserved. 
   	Permission is granted for use in non-commercial applications provided 
	this copyright notice remains intact and unchanged.
*/
  
#include<stdio.h>
 
/* **********************************	*/
/*     Convert number 1 to 10  		*/
/*     Input  : A integer number   	*/
/*     Return : None			*/
/* **********************************	*/
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  	*/
/*     Input  : A integer number   	*/
/*     Return : None			*/
/* **********************************	*/
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  	*/
/*     Input  : A integer number   	*/
/*     Return : None			*/
/* **********************************	*/
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 	*/
/*     Input  : A integer number   			*/
/*     Return : None					*/
/* *************************************************	*/
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); 
} 
/* Sample  main() function */ 
main(void){		roman(390);	return 0; 	} 


    
    
 

Copyright 2003 M H Rasel
[
Webmaster]
Last modified

1