Click here to Skip to main content
12,350,597 members (24,928 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C
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 17-Feb-13 19:04pm
Edited 17-Feb-13 19:14pm
v2
Comments
   
What do you mean "generic"? This is not Ada, not .NET...
—SA
abhaybudakoti 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.
   
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
   
Not a question, anyway...
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v2
Comments
abhaybudakoti 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi

Check the following link, Clearly explained.


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

Regards,
Venkatesh.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:

char* Convert(int inputBase, int OutputBase, char* input);
or, more correct in C way, to avoid the problem of string deallocation:
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. Smile | :)

—SA
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
abhaybudakoti 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.
abhaybudakoti 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

#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
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100