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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121`` ``````#include #include 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
 ``12`` ``````#include //strlen #include //pow ``````

Last edited on
Topic archived. No new replies allowed.