# Help with decimal to hexidecimal program

• 12-22-2001
CheeseMonkey
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
• 12-22-2001
Unregistered
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.