hexadecimal conversion question

This is a discussion on hexadecimal conversion question within the C++ Programming forums, part of the General Programming Boards category; I need to write a class (function) where I can change a hexadecimal to an integer and return the value. ...

  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, 08:33 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  3. Help with Hexadecimal Conversion
    By f4ichick02 in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 11: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, 09:39 AM

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