Converting decimal to hex

This is a discussion on Converting decimal to hex within the C++ Programming forums, part of the General Programming Boards category; I've been writing a program that will convert a decimal input to an octal or hex number. Basically I just ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Converting decimal to hex

    I've been writing a program that will convert a decimal input to an octal or hex number. Basically I just need some help with the decimal to hex conversion. I searched the board for a while, but couldnt find anything...(I'm new to C++, so it might be that i just dont understand it.) Here's my code so far, thanks for any help!

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    enum ConverstionType {NONE,DECIMAL,HEX,OCTAL};
    int number = 0;// a global variable visible throughout the file
    int test = NONE;
    
    // this function converts a char to upper case 
    // and display it if not a digit representation;
    void asciiToInt(char aChar){
    	int digitValue = 0;
    	switch(::test) {
    		case OCTAL: {
    					 digitValue = aChar - '0';
    					::number *= 8;
    					::number += digitValue;
    					break;
    		}
    		case DECIMAL:
    					{
    					 digitValue = aChar - '0';
    					::number *= 10;
    					::number += digitValue;
    					break;
    					 }
    		case HEX:
    				{if (aChar >= 'a' && aChar <= 'f')
    					digitValue = aChar - 'a' + 10;
    				else
    				digitValue = aChar - '0';
    				::number *= 16;
    				::number += digitValue;
    				break;
    				 }
    
    		default: {
    			cout << "error in conversion" << endl;
    			::number = 0;
    		}
    	}
    }
    
    int main(){
    string word;
    cout << "enter a number:";
    getline(cin,word);
    	if(word[0] == '0')
    		::test = OCTAL;
    	else if(word[1] == 'x')
    		::test = HEX;
    	else
    		::test = DECIMAL;
    cout << word << endl;
    
    // apply the function showChar to each character in the string
    for_each(word.begin(),word.end(),asciiToInt);
    cout << "converted number is " << number << endl;
    return 0;
    }

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    How about..

    long number;
    std::cout << "Please enter a number: ";
    std::cin >> number;

    std::cout << "\nIn hex: " << std::hex << number;
    std::cout << "\nIn oct: " << std::oct << number;
    std::cout << "\nIn dec << std::dec << number;

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Thanks a lot! Thats a whole lot easier. I just dont know if my professor will take it. Know of a way to do it with the code that i started with??

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Well, you're going to need a carry value from time to time when converting from a higher to lower base... or if you're converting from lower to higher base you'll need a borrow. You could search planet-source-code.com for examples of converting between hexadecimal/decimal/octal

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Thanks for the help. There's a lot of good stuff on there too. Can someone tell me how to use a carry/borrow value? Like I said, I'm really new to this.

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Cool

    What do you mean by "carry and borrow"
    Mr. C: Author and Instructor

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Thats the problem...I dont really know. Refer to Eibro's post.

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Mister C
    What do you mean by "carry and borrow"
    Nevermind, I guess I didn't look at his code closely enough.

  9. #9
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Know of a way to do it with the code that i started with??
    Yes, I also know of a better way. Here's a function that does what you want :-)
    Code:
    #include <algorithm>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string hex_value(int decimal)
    {
      static const string hex_digits("0123456789ABCDEF");
    
      string hex;
      int scratch = decimal;
    
      while (scratch != 0)
      {
        hex += hex_digits[scratch % 16];
        scratch /= 16;
      }
    
      reverse(hex.begin(), hex.end());
    
      return hex;
    }
    
    int main()
    {
      cout<< hex_value(27) <<endl;
    }
    *Cela*

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Talking

    Thanks for everyone's help. I finally got it working using the form my professor wants. If anybody's interested, here's my final code. Thanks again.

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    enum ConverstionType {NONE,DECIMAL,HEX,OCTAL};
    int number = 0;// a global variable visible throughout the file
    int test = NONE;
    
    // this function converts a char to upper case 
    // and display it if not a digit representation;
    void asciiToInt(char aChar){
    	int digitValue = 0;
    	switch(::test) {
    		case OCTAL: {
    					 digitValue = aChar - '0';
    					::number *= 8;
    					::number += digitValue;
    					break;
    		}
    		case DECIMAL:
    					{
    					 digitValue = aChar - '0';
    					::number *= 10;
    					::number += digitValue;
    					break;
    					 }
    		case HEX:
    				{if (aChar >= 'a' && aChar <= 'f')
    					digitValue = aChar - 'a' + 10;
    					else if (aChar == 'x')
    					digitValue = 0;
    				else
    				digitValue = aChar - '0';
    				::number *= 16;
    				::number += digitValue;
    				break;
    				 }
    
    		default: {
    			cout << "error in conversion" << endl;
    			::number = 0;
    		}
    	}
    }
    
    int main(){
    string word;
    cout << "Enter a number to convert to decimal form:" << endl;
    cout << "Octal numbers should be preceded by a 0." << endl;
    cout << "Hexadecimal numbers should be preceded by an x." << endl;
    
    getline(cin,word);
    	if(word[0] == '0')
    		::test = OCTAL;
    	else if(word[0] == 'x')
    		::test = HEX;
    	else
    		::test = DECIMAL;
    cout << word << endl;
    // apply the function showChar to each character in the string
    for_each(word.begin(),word.end(),asciiToInt);
    cout << "The Converted number is " << number << endl;
    cout << " " << endl;
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 06:17 PM
  2. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 01:14 AM
  4. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM
  5. converting a hex number to decimal???
    By Unregistered in forum Game Programming
    Replies: 8
    Last Post: 04-13-2002, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21