Converting Integers to Hex...

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Converting Integers to Hex...

1. Registered User
Join Date
Aug 2005
Posts
1

## 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";
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

2. 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.

3. Registered User
Join Date
May 2005
Location
Usa
Posts
53
For C++ style, you can do this to convert decimal base to hex, oct base.
cout<<hex<<number<<endl; // hex base
cout<<oct<<number<<endl; // oct base

4. Senior Member
Join Date
Dec 2003
Posts
3,366
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.

5. 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).

6. Senior Member
Join Date
Dec 2003
Posts
3,366
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...

7. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise