Thread: hexadecimal conversion question

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Question hexadecimal conversion question

    I need to write a class (function) where I can change a hexadecimal to an integer and return the value. I have no idea where to begin. Can anyone help me?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    First an integer stores its values in binary or twos complement if signed. You don't convert from hexadimal to integer. An integer is an integral number and hexadecimal is a base 16 representation of a value. Therefore its only important how you display it. Hexadecimal or Decimal or whatever. I think you want to call a funtion with an int and return an ascii string showing the value in hex. I could be all wet but this should get you started.

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    
    	int i = 100;
    	char HexChar[80];
    
    	cout << "i hex = " << itoa( i, HexChar, 16 ) << endl;
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Code:
    #include <cstdio>
    
    class _hextodec
    {
    public:
        int operator()(const char* hex) const
        {
            int retval;
            sscanf(hex, "%x", &retval);
            return retval;
        }
    } hextodec;
    
    int main()
    {
        char* hex = "a8b1";
    
        printf("%s = %d\n", hex, hextodec(hex));
    
        return 0;
    }
    - lmov

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Thank you :)

    Thank you both so much, this has helped me out quite a bit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Help with Hexadecimal Conversion
    By f4ichick02 in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 10:37 PM
  4. Quick Conversion Question
    By DougC in forum C Programming
    Replies: 3
    Last Post: 01-10-2003, 07:12 PM
  5. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 08:39 AM