conversion

I have written program to do conversion between decimal to any base between 2 to 26..
but I cant handle alphabets as alphabets are also included in this conversion.

plz help..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream.h>
#include<conio.h>

void conversion(int no,int base)
{
	int arr[10]={0};
	int i=0;
	while(no>0)
	{
		arr[i]=no%base;
		i++;
		no=no/base;

	}
	for(int j=i;j>=0;j--)
		cout<<arr[j];
}
void conversionGreater(int no,int base)
{
	int arr[20]={0};
	int i=0,j=0;
	char a='A';

	while(no>0)
	{
		arr[i]=no%base;
		i++;
		no=no/base;

		if(no>=10)
		{
			j=no%base;

			for(int k=0;k<j;k++)
				arr[i]=a+k;
			i++;
			no=no/base;
		}

	}
	for(int l=i;l>=0;l--)
		cout<<arr[l];
}
void main()
{
	clrscr();
	int base=16;
	int no=2600;
	if(base >=2 && base<=9)
		conversion(no,base);
	else if(base >=11 && base<=26)
		conversionGreater(no,base);
	getch();
}
There's no difference between the two cases.

Make the following string:

"0123456789abcdef....z" (actually this gives you up to base 36).

After you do the % to get the "digit", use the computed value to index into the string to find its character representation.

You can actually use this same technique to further generalize your program to handle allowing the user to input a number in any base and convert to any other base.

can you explain it
what you have said I do not understand it:(
In your conversion function, for example, you do a modulo (%).
The result of the modulo is the converted "digit" which is obviously a number.

In your conversionGreater function, as you point out, sometimes the modulo returns a number > 9, which you correctly recognize as being a letter, not a number. But you have an array of ints, and you can't really store letters there.

So what I'm saying is to change your array of ints to an array of chars.
Make the string I mentioned above. Now the [0]th index into the string is the character '0', the [1]st index is '1', and so forth. The [10]th index is 'a', which is the character that you want to print if the result of the modulo is 10.

So each time you perform the modulo, you'll get a number between 0 and (base-1). Use that number to index into the string to find the character you need to print.
check this function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void conversionGreater(int no,int base)
{
   int arr[20]={0};
   char a[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   int no1=0;
	while(no>0)
	{
		no1=no%base;
		cout<<no%base;
		no=no/base;
		no1=no1-10;
		if(no1>=10)
		{
			cout<<a[no1];
		}
	}
}
Topic archived. No new replies allowed.