Help with decimal to hexidecimal program

This is a discussion on Help with decimal to hexidecimal program within the C++ Programming forums, part of the General Programming Boards category; Well, I want to thank the people that helped me with my previous number conversion programs, but now I have ...

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

    Arrow Help with decimal to hexidecimal program

    Well, I want to thank the people that helped me with my previous number conversion programs, but now I have another that I have some problems with. I've been working on this decimal to hexidecimal program. It outputs strange characters. However, it gives the correct answer with numbers from 0 to 15. Any help would be greatly appreciated.

    Code:
    #include <iostream.h>
    #include <math.h>
    
    //Converts the decimal to a hexidecimal number which appears as a string
    //Uses the math.h header file
    void dectohex (unsigned long int num, char *phex)
    {
    	char hexnumbers[] = {'0', '1', '2', '3', '4', '5', '6', '7',
       							'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
       int count = 0;
    
       for(int x = 7; x > -1; x--)
       {
       	if (pow(16, x) < num)
          {
          	for(int y = 0; y < 16; y++)
             {
             	if (pow(16, x) * y > num)
                {
                	phex[count] = hexnumbers[y];
                   num = num - pow(16, x) * y;
                   count++;
                }
                else if (pow(16, x) * y == num)
                {
                	phex[count] = hexnumbers[y];
                   phex[count + 1] = '\0';
                   return;
                }
             }
          }
          else if (pow(16, x) == num)
          {
             phex[count] = hexnumbers[1];
    
          	for(int c = count; c >= x; c++)
             {
                phex[c] = hexnumbers[0];
             }
    
             phex[count + x + 1] = '\0';
             return;
          }
       }
    }
    
    //Main function
    
    int main(void)
    {
    	char hexstring[7];
       unsigned long int number;
    
       cout << "Enter a number: ";
       cin >> number;
    
       dectohex(number, hexstring);
       cout << "The hexadecimal number is: " << hexstring;
    
       return 0;
    }
    Thank you
    Last edited by CheeseMonkey; 12-22-2001 at 02:06 PM.

  2. #2
    Unregistered
    Guest
    Cstyle:
    printf("%x", number);

    C++style
    cout<<hex<<number;

    I believe that's what the commands are, but you'll want to try them and see.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Decimal to Binary Conversion program
    By acidbeat311 in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 10:26 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. Replies: 4
    Last Post: 01-29-2002, 02:42 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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