signed hex to dec converter

When I run this program for converting Dec to Hex works perfect, but in Hex to Dec, I get a debug error in VC++ runtime library but still get the right answer. Anybody has and idea?

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
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <string>

using namespace std;

void To_Hexadecimal(int);
int To_Decimal(char[]);

int main(){
		char Hex[2];
	cout << "Enter 1 for Hex to Dec or 2 for Dec to Hex: "  ;
	int x = 0;
	cin >> x;

	if( x == 1 ){
		cout << "Enter a hex number: ";
		cin >> Hex;
		cout << "Decimal: " << To_Decimal(Hex)<< endl;

	int decimal = -1;
	if (decimal != -1)
		cout << "The decimal value is: " 

<< decimal << endl;
	}

	if (x == 2){
		int decNum = 0;
		cout <<"Enter Decimal Number: ";
		cin >> decNum;
		cout << "Hexadecimal: "  ;
				To_Hexadecimal(decNum);
		cin.clear();
		cin.sync();
	}

	return 0;
}//main

void To_Hexadecimal(int n){
	bool negsign = false;
	int result[2]={0,0};
	int remain[2]={0,0};
	if(n < 0 ){
		negsign = true;
		n= -n;
	}
	for(int i = 2 ; i > 0 ; i--){
		remain[i-1] = n % 16;
		n /= 16;
	}
	if(negsign){
		int sub[2]={15,16};
		for(int i = 1 ; i >= 0 ; i--)
			result[i] = sub[i] - remain[i];
	}
	else
		for(int i = 1 ; i >= 0 ; i--)
			result[i] = remain[i];

	for(int j = 0 ; j < 2 ; j++){
		if(result[j] < 10)
			cout << result[j];
		else
			if(result[j] == 10)
				cout << "A";
		else
			if(result[j] == 11)
				cout << "B";
		else
			if(result[j] == 12)
				cout << "C";
		else
			if(result[j] == 13)
				cout << "D";
		else
			if(result[j] == 14)
				cout << "E";
		else
			if(result[j] == 15)
				cout << "F";
	}
	cout << endl;
}//To Hex


int To_Decimal( char Hexadecimal[]){
	bool negsign = false;
	int result[2];
	int n = 0;
	if((Hexadecimal[0] >= '8' && Hexadecimal[0] <= '9' && strlen(Hexadecimal) > 1)
		|| (Hexadecimal[0] >= 'A' && Hexadecimal[0] <= 'F' && strlen(Hexadecimal) > 1))
		negsign = true;
	if(negsign){
		double a = 0.0;
		int sub[2]={15,16};
		int temp[2]={0,0};
		for(int i = 0 ; i < strlen(Hexadecimal) ; i++)
			if( Hexadecimal[i] >= '0' && Hexadecimal[i] <= '9' )
			temp[i] = Hexadecimal[i] - '0';
		else if(Hexadecimal[i] >= 'A' && Hexadecimal[i] <= 'F')
			temp[i] = (Hexadecimal[i] - 'A') + 10 ;
		for(int i = 0 ; i < 2 ; i++){
			result[i] = sub[i] - temp[i];
			a = strlen(Hexadecimal)-1-i;
			n = n + (int)(result[i] * (pow(16,a)));
		}
	}
	else
		for( int i= 0 ; i < strlen(Hexadecimal) ; i ++){
			int temp;
			double a;
			if( Hexadecimal[i] >= '0' && Hexadecimal[i] <= '9' )
				temp = Hexadecimal[i] - '0';
			else if(Hexadecimal[i] >= 'A' && Hexadecimal[i] <= 'F')
				temp = (Hexadecimal[i] - 'A') + 10 ;
			a = strlen(Hexadecimal)-1 -i;
			n = n + (int)(temp * (pow(16,a)));	
		}
	return ((negsign)? (-n) : n);
}//To Decimal 
First problem that this is not a conversion to signed Hex. It looks like you are trying to convert to minimum size two's complement hex. Because of that there is double meaning: both 156 and -100 will be 9C in your converter. Hint: -15 will be -F in signed hex.

Aside from that you forgot to include
1
2
#include <cstring> //strlen
#include <cmath> //pow 

Last edited on
Topic archived. No new replies allowed.