# converting a hex number to decimal???

• 04-08-2002
Unregistered
converting a hex number to decimal???
Does anyone know any quick way to convert a hex number to a decimal
• 04-08-2002
ihsir
use a char array

Code:

```char hex[9]; inthex[9]; cout<<"Enter hex  : "; cin.getline(hex,8,'\n');```
compare the input and write the corresponding number in an int array, use switch-case to compare and for to traverse the array
Code:

```for(i=0;i<8;i++)  switch(hex[i])  {   case 1:     inthex[i]=1;   break;   case A:   inthex[i]=10;   :   :  }```
do the hex to dec calculations

PS this is just some basic steps not actual code stuff, if it doesn't work post again... or do a search i think it has been asked before
:)
• 04-09-2002
xds4lx
This isnt that hard, heres a simple solution, store the hex representation as a string ex: "7AE". The way you convert to dec is go through a loop examining each character then you multiply the value in that index by 16^index. Oh yeah if you do it this way make sure you flip the string around since you convert from the right to left. ex:
Code:

```int Hex_to_Dec(string strVal) {  int Val = 0;     ReverseString(strVal); // you have to write this urself i just did this by pass by reference so i could modify the string in the function     for(int cIndex = 0; cIndex < strVal.length(); cIndex++)     {  char chVal = strVal[i];       if((chVal > '0' || chVal < '9'))       {        Val += pow(HEX,cIndex) * atoi(Val);       }       else       {  switch(chVal)           {        case 'a':                 case 'A':                 {        Val+= pow(HEX,cIndex) * 10;                         break;                 }                  case 'b':                 case 'B':                 {        Val+= pow(HEX,cIndex) * 11;                         break;                 }                 case 'c':                 case 'C':                 {        Val+= pow(HEX,cIndex) * 12;                         break;                 }                 case 'd':                 case 'D':                 {        Val+= pow(HEX,cIndex) * 13;                         break;                 }                 case 'e':                 case 'E':                 {        Val+= pow(HEX,cIndex) * 14;                         break;                 }                 case 'f':                 case 'F':                 {        Val+= pow(HEX,cIndex) * 15;                         break;                 }               } // end of switch           } // end of else       } // end of for       return(Val); }```
• 04-12-2002
Unregistered
hexatodecimal
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
void main(void)
{
clrscr();
char convert[20]={0};
int length,count=0,sum=0;
printf("Enter the number= ");
gets(convert);
length=strlen(convert);
for(int i=length-1;i>=0;i--)
{
if(convert[i]>=48&&convert[i]<=57)
convert[i]-=48;
else if(convert[i]>=65&&convert[i]<=70)
convert[i]-=55;
else if(convert[i]>=97&&convert[i]<=102)
convert[i]-=87;
sum+=convert[i]*pow(16,count);
count++;
}
cout<<sum<<endl<<itoa(sum,convert,2);
getch();
}
• 04-12-2002
Unregistered
How would you convert hex to dec from a saved file*I made it so that when you save, the values are in hex........and I can't turn them back so that they will work*
• 04-12-2002
xds4lx
Why not read them in as strings? Store them then use the previous code to manipulate them? If that doenst work then we need to see some code where you are having problems.
• 04-12-2002
Quantrizi
I'm the unregistered person above about the save file problem. When the user saves a file, it has the 0x, and then the hex values. But I can't get it to where when the user loads the file, it's back to it's original numbers.
• 04-13-2002
xds4lx
Well how are you reading the file? Into a char array or as strings? Or you could just read them in as ints, the 0x will be interpreted into a value since you can specify values by hex or oct
• 04-13-2002
Quantrizi
they're int's.