Converting decimal number to octal,binary,hexadecimal appears backwards.

I have to write a program that converts a decimal number to a binary,octal, or hexadecimal number. The number are coming out correct but backwards. Like if the answer should be 123, it comes out as 321. I've been trying to fix this for a long while now and can't seem to get the right code. Any help would be appreciated.

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
 #include<iostream>
using namespace std;

int main()
{
	int decimal, remainder, choice;
	cout << "Please enter a decimal: ";
	cin >> decimal;
	cout << "Convert the number from decimal into:\n0  Binary\n1  Octal\n2  Hexadecimal\n";
	cin >> choice;
	switch (choice)
	{
	case 0:
		while (decimal > 0)
		{
		remainder = decimal % 2;
		decimal /= 2;
		cout << remainder;
		}
		break;
	case 1:
		while (decimal > 0)
		{
		remainder = decimal % 8;
		decimal /= 8;
		cout << remainder;
		}
		break;
	case 2:
		while (decimal > 0)
		{
			remainder = decimal % 16;
			decimal /= 16;
			if (remainder > 9)
				cout << char('A' + remainder - 10);
			else
				cout << remainder;
		}
		break;
	default:
		cout << "Invalid.";
	}
	return 0;
}
Last edited on
As you are going to derive the digits from the right, but write them back from the left, you probably have to store them in something (e.g. a string as below), adding each new one to the left, before you write them out as below.


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
#include<iostream>
#include<string>
using namespace std;

int main()
{
        string converted ="";        // stores result; appending to start for each new digit
	int decimal, remainder, choice;
	cout << "Please enter a decimal: ";
	cin >> decimal;
	cout << "Convert the number from decimal into:\n0  Binary\n1  Octal\n2  Hexadecimal\n";
	cin >> choice;
	switch (choice)
	{
	case 0:
		while (decimal > 0)
		{
		remainder = decimal % 2;
		decimal /= 2;
		converted = char( remainder + '0' ) + converted;
		}
		break;
	case 1:
		while (decimal > 0)
		{
		remainder = decimal % 8;
		decimal /= 8;
		converted = char( remainder + '0' ) + converted;
		}
		break;
	case 2:
		while (decimal > 0)
		{
			remainder = decimal % 16;
			decimal /= 16;
			if (remainder > 9)
				converted = char('A' + remainder - 10) + converted;
			else
		                converted = char( remainder + '0' ) + converted;
		}
		break;
	default:
		cout << "Invalid.";
	}

        // Now write result
        cout << converted;

	return 0;
}



Note: you could also improve your code quite a lot by shortening it. You essentially have the same operation written out three separate times (but for different base b, where b is 2,8 or 16). You could put the switch block inside here. There is nothing stopping you using any other number base.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>
using namespace std;

int main()
{
   string converted ="";     // stores result, appending to start for each new digit
   int decimal, remainder, base;
   char offset;

   cout << "Please enter a decimal and the number base to convert to: ";
   cin >> decimal >> base;

   while (decimal > 0)
   {
      remainder = decimal % base;
      decimal /= base;
      offset = remainder <= 9 ? '0' : 'A' - 10;
      converted = char( remainder + offset ) + converted;
   }
   cout << converted;
   
   return 0;
}
Last edited on
Topic archived. No new replies allowed.