Does anyone know any quick way to convert a hex number to a decimal
Does anyone know any quick way to convert a hex number to a decimal
use a char array
compare the input and write the corresponding number in an int array, use switch-case to compare and for to traverse the arrayCode:char hex[9]; inthex[9]; cout<<"Enter hex : "; cin.getline(hex,8,'\n');
do the hex to dec calculationsCode:for(i=0;i<8;i++) switch(hex[i]) { case 1: inthex[i]=1; break; case A: inthex[i]=10; : : }
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
Last edited by ihsir; 04-08-2002 at 11:39 PM.
-
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); }
"only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein
#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();
}
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*
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.
"only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein
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.
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
"only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein
they're int's.