converting decimal to hexadecimal

Hello all. So this was a code I've been working on and trying to implement another feature to this switch program. I'm trying to add a hexadecimal conversion. My try at it starts at line 74-94. I was looking at other converting codes written by other people and I know I need to install:
1
2
3
4
5
6
7
8
9
10
11
12
if (y==10)
       cout << 'A';
    else if (y==11)
       cout << 'B';
    else if (y==12)
       cout << 'C';
    else if (y==13)
       cout << 'D';
    else if (y==14)
       cout << 'E';
    else if (y==15)
       cout << 'F';

somewhere into my code. Also, I have to deal with unexpected inputs other than what my switch is asking for. So I have to add throwing exceptions in my code to deal with them. I have no idea how these work. Any help is great, my code is posted 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <stdexcept> //Exception handling.
using namespace std;


int main()
{
	
	int bArray[16] = { 0 };
	int k = 0;

	int oArray[8] = { 0 };
	int i = 0;

	int hArray[16] = { 0 };
	int j = 0;

	int decimal = 0;
	double binary = 0;
	double octal = 0;
	int number;

	cout << "Enter 'B' to convert a decimal number to binary, 'O' for an octal conversion,and'H' for a hexadecimal conversion. "
		<< "Hit space to quit program." << endl;

	while ((number = cin.get()) != ' ')
	{
		cout << "\nEnter a decimal number to be converted (No more than 65,535): " << endl;

		switch (number)
		{
		case 'B':
		case 'b':
			cin >> decimal;
			while ((decimal != 0) & (k <= 15))
			{
				bArray[k] = decimal % 2;
				decimal = decimal / 2;
				k++;
			}

			cout << "Here is the binary conversion: ";

			for (k = 15; k >= 0; k--)
			{
				cout << bArray[k];
				if ((k % 4) == 0)
					cout << " ";
			}
			cout << endl;
			break;

		case 'O':
		case 'o':
			cin >> decimal;
			while ((decimal != 0) & (i <= 7))
			{
				oArray[i] = decimal % 8;
				decimal = decimal / 8;
				i++;
			}

			cout << "Here is the octal conversion: ";

			for (i = 7; i >= 0; i--)
			{
				cout << oArray[i];
				if ((i % 4) == 0)
					cout << " ";
			}
			cout << endl;
			break;

		case 'H':
		case 'h':
			cin >> decimal;
			while ((decimal != 0)&(j <= 15))
			{
				hArray[j] = decimal % 16;
				decimal = decimal / 16;
				j++;
			}

			cout << "Here is the hexadecimal conversion: ";

			for (j = 15; j >= 0; j--)
			{
				cout << hArray[j];
				if ((j % 4) == 0)
					cout << " ";
			}
			
			cout << endl;;
			break;

		case '\n':
		case '\t':
		case ' ':
			break;

		default:
			cout << "Wrong decimal input." << "Enter a new number." << endl;
			break;
		}
	}

	cout << "Thank you." << endl;//end main
}
This is a simple way to do it here. I can guarantee you it's not the most efficient but it's easy to code.

I'm sure you could work out a way to divide by 16 until 0 then loop through the remainder to get an exact value and save processor time.

Hopefully this gives you some idea on how to do it.

This basically subtracts one from the user input decimal and adds one to a hexidecimal array. Then when one of the hex array elements exceeds 15 it increments the next higher place value and sets the current place value to 0. The program loops through until user input has reached zero.

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

int main()
{
	int hex_array[8];
	int user_input;

    for (int i = 0; i < 15; i++){
        hex_array[i] = 0;//populate hex_array with 0's
        }

cout << "Enter a decimal number to convert to hexidecimal: ";

cin >> user_input;

    for (int i = user_input; i > 0; i--){
            hex_array[7] = hex_array[7] + 1;
            if (hex_array[7] > 15){
                for (int n = 6; n > -1; n--){
                        hex_array[n+1] = 0;
                        hex_array[n] = hex_array[n] + 1;
                        if (hex_array[n] < 16){
                            n = -1;
                        }
                    }
                }
    }
cout << "\n\nConverted to Hexidecimal is: ";
for (int i = 0; i < 8; i++){
        if (hex_array[i] == 10){cout << 'A';}
        else if (hex_array[i] == 11){cout << 'B';}
        else if (hex_array[i] == 12){cout << 'C';}
        else if (hex_array[i] == 13){cout << 'D';}
        else if (hex_array[i] == 14){cout << 'E';}
        else if (hex_array[i] == 15){cout << 'F';}
        else {cout << hex_array[i];}
        }

return 0;

}
Last edited on
Topic archived. No new replies allowed.