# Converting Integers to Hex...

• 08-02-2005, 02:48 PM
mattbiegner
Converting Integers to Hex...
Ok, I'm having a little trouble with my code. I have it all setup to convert from decimal to Binary, Octal, and hex. However, with hex I get 1515 and not FF. Even though that would technically be right, I need to make it show FF. Here's my code thus far:

Code:

```//Exercise 10 //Matt Biegner #include <iostream> using namespace std; void convert(int number, int base); int main() {         int number;         int option;         cout << "Enter an integer in decimal: ", cin >> number, cout << endl;         cout << "Convert from decimal into:\n";         cout << "0 - Binary\n";         cout << "1 - Octal\n";         cout << "2 - Hexadecimal\n";         cin >> option, cout << endl;         switch (option)         {         case 0:                 convert(number, 2);                 break;         case 1:                 convert(number, 8);                 break;         case 2:                 convert(number, 16);                 break;         }         return 0; } void convert(int number, int base) {         int result = 0;         int store[35];         for(int i = 0; i < number;){                                ++result;                         store[result] = number % base;                         number /= base;}                 while(result > 0){                         cout << store[result];                         --result;                         }         cout << endl << endl; }```
----
Matt Biegner
• 08-02-2005, 10:33 PM
Danny
Your convert function can use either the C-style format flags: %x, %d, %o to display the output in the correct format (except binary -- this one you'll have to implement manually) or you can use the C++ iomanip library which has manipulators such as hex, dec, oct, etc.
• 08-02-2005, 10:55 PM
Butterfly
For C++ style, you can do this to convert decimal base to hex, oct base.
Quote:

cout<<hex<<number<<endl; // hex base
cout<<oct<<number<<endl; // oct base
• 08-04-2005, 09:58 AM
jonnin
I so can't stand that -- what is the variable 'hex' ? Oh its not a variable its the same syntax used differently to confuse the reader. Not your fault for using it. But the standards guys had there head in the sand when they did that.
• 08-04-2005, 09:46 PM
Danny
I don't see a difference between 'hex' and say 'endl'. These are function names that are passed to the stream object. I think they are more readable and intuitive than say "%x" or "%h" (the latter btw means 'short', heaven knows why).
• 08-05-2005, 08:02 AM
jonnin
I dont like endl either for the same reason. They are more readable than sprintf but you cant tell variables from commands. Sprintf puts the % on everything, which leaps out to the eye. cout << %hex << number; --- much better. Anything to say its not a variable would be better. %h means sHort probably, because s was used already -- again not terribly bright to use one letter for so long a list of types. I have also had cout (rarely) confuse << with shift and had to explicitly call operator <<, but thats another story...
• 08-06-2005, 11:01 PM
Danny
OK, I'm not saying that it was the wised design descision but making hex, oct and other manipulators look like variables was deliberate. Think about the following code:

cout<<x;

without knowing what x is, you expect this statement to display x. Now if x happen to be a manipulator such as hex, the same applies here: cout, in a way, this "variable" and displays it on the screen except that it's not a numeric value but a format flag that affects the output. I do agree about the << and >> operators though. Not because they can be mistaken for a bit shift operator (any overloaded oerator would be mistaken for its built-in counterpart at some point) but because it's too long. It takes two characters which doesn't seem a lot but when you chain multiple values in a single statement, it really becomes cumbersome. Add to it the std:: prefixed to cout, end and other mnipulators, and you end up with cluttered up code.