12,350,597 members (24,928 online)

Email

Password

Sign in using

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.

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.

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

In base of 9 (65) = ? in decimal.

Comments

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.

v2

Comments

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

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).

You still need to solve the other half of the problem (ie. how to convert ascii "47" to base 9).

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.

Check the following link, Clearly explained.

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

Regards,

Venkatesh.

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:

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

`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.

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.

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.

Comments

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

I am suggesting that as a way to learn how to write the function gernerically, rather than just have someone give you the code.

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.

```
#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

v2

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

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

—SA

—SA

—SA