Thread: converting a hex number to decimal???

  1. #1
    Unregistered
    Guest

    Question converting a hex number to decimal???

    Does anyone know any quick way to convert a hex number to a decimal

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    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
    Last edited by ihsir; 04-08-2002 at 11:39 PM.
    -

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    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

  4. #4
    Unregistered
    Guest

    Talking 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();
    }

  5. #5
    Unregistered
    Guest
    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*

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    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

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    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.

  8. #8
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    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

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    they're int's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 0x0F stuff
    By Overlord in forum C++ Programming
    Replies: 11
    Last Post: 08-15-2006, 09:59 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  4. hex to decimal
    By caroundw5h in forum C Programming
    Replies: 5
    Last Post: 11-11-2004, 10:48 AM
  5. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 11:37 AM