Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Generic function in which i can change any number system suppose hex number into decimal or octal number into decimal.

Code should be valid for other number system also like if we want to convert a number of base 12 or base 9 into decimal number system.

In hex 38 = 56 in decimal.
In octal 142 = 98 in decimal.


NOTE Base could be any number between 2 to 16.

But what it will give me when i convert 65 of base 9 into decimal.

In base of 9 (65) = ? in decimal.
Posted
Updated 17-Feb-13 19:14pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 1:10am    
What do you mean "generic"? This is not Ada, not .NET...
—SA
Abhay Budakoti 18-Feb-13 1:19am    
Generic means your function should work for any number system. it should convert octal to decimal and hex to decimal and also from base of 6 or 9 to decimal number system.
Sergey Alexandrovich Kryukov 18-Feb-13 1:22am    
Sure. You can make it in a "generic" way by a simple reason: you are not working with numbers, but you are working with strings.
—SA
Sergey Alexandrovich Kryukov 18-Feb-13 1:10am    
Not a question, anyway...
—SA

Internally, a char/int/float/double value is always stored the same way regardless of what "base" we think the number is in. What you are talking about is how to display some binary value in another base system. You want to look at the crt help for _ltoa_s()[^], _ultoa_s()[^] and related functions. Pay attention to the radix parameter.
 
Share this answer
 
v2
Comments
Abhay Budakoti 18-Feb-13 2:20am    
Thanks for replay but i am not taking about display some binary value in another base system. i can do that by using format specifiers but it is possible only with standard number system like HEX AND OCTAL because there is predefined format specifier for them like %x and %o. But i do not want to display, i want to convert number of base 9 or 6 or 3 or 5. than what should i do
H.Brydon 18-Feb-13 9:18am    
Well yeah, actually this solves half of the problem. Suppose you have a number (eg. 47) and want it displayed/converted to base 7. You can use one of these functions to do so.

You still need to solve the other half of the problem (ie. how to convert ascii "47" to base 9).
H.Brydon 18-Feb-13 15:40pm    
Assuming you are storing your values in an 'int' value, solution #6 shows you how to parse a character version of the number into an 'int' and solution #3 shows you how to go the other way.
Hi

Check the following link, Clearly explained.


Conversion of numbers from one to another number system[^]

Regards,
Venkatesh.
 
Share this answer
 
There is nothing "generic" in such function, or, in a way, the only one function is "generic enough". Here is one possible signature of such function:

C++
char* Convert(int inputBase, int OutputBase, char* input);

or, more correct in C way, to avoid the problem of string deallocation:
C++
void Convert(int inputBase, int OutputBase, char* input, char* output);

In the second case, the called should supply both char pointers preserving sufficient room for output value.

This is so because (please see my comment to the question), the notion of the base is only applicable to strings representing numbers, not the numbers (you can think of numbers as always binary). You just need to make a string based on input string. The body of this function is your home exercise. :-)

—SA
 
Share this answer
 
Here's how I would proceed with this homework assignment;

Write a routine to convert from (say) base 4 to base 8 (choose any two)

Now write another to convert from base 4 to base 16

Keep picking pairs of numbers and writing the routines to convert between them.

Once you have done it a few times, you will find yourself writing lots of code again and again - indeed you can cut and paste much of it.

Now, start thinking about how to make it more generic - instead of hard-coding any numbers, have them as parameters to the method.

when you get that far, if you have specific issues, please don't hesitate to ask again for help.
 
Share this answer
 
Comments
Abhay Budakoti 18-Feb-13 2:34am    
thanks for your suggestion but i just want to make it generic means i do not want to Keep picking pairs of numbers and writing the routines to convert between them. just want write one function which is capable of converting any number system to any number system between base of 2-16
_Maxxx_ 18-Feb-13 2:51am    
I am suggesting that as a way to learn how to write the function gernerically, rather than just have someone give you the code.
Abhay Budakoti 22-Feb-13 1:03am    
hey maxxx i appreciate you for your valuable suggestion, and i will try to think as you suggest me. I tried to make some generic code for it. I already post it as answer, we can still do more in that code.
C++
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

int numInDec(char *numStr,int base)
{
	int number;
	int ch;
	char sh[100];
	int i=0,j=0;
 
	printf("input No in Hex: ");
	//scanf("%s",&sh);
	strcpy(sh,numStr);
	number = 0;
	i =strlen(sh);
	j=0;
	while(i!=0)
	{
		ch = sh[j];
		ch = toupper(ch); 
		printf("Ch:%d\t%d\t%d\n",ch,'7',ch-'7');
 
		if(('0' <= ch && ch <= '9'))
		{
			number = number * base;
			printf("Number :%d\n",number);
			number = number + (ch - '0');
		}
 
		if(('A' <= ch && ch && ch <= 'Z'))
		{
			number = number * base;
			printf("Number :%d\n",number);
			number = number + (ch - '7');
		}
 
		printf("Number :%d\n",number);
		i--;
		j++;
 
	}
 
	printf("The O/p No in Decimal is : %d\n",number);
	return number;
}
 
int main()
{
   int n1=0,n2=0,n3=0,n4=0;
   n1 = numInDec("1430",5);
   n1 = numInDec("110",2);
   n1 = numInDec("2A",12);
   n1 = numInDec("10",10);
   printf("\n%d %d %d %d\n",n1,n2,n3,n4);
   return 0;
}


Here is my solution, i just pass number as a string and its base as a integer.
NOTE This code required error handling
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900