-
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
-
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.
Danny Kalev
-
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
-
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.
-
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).
Danny Kalev
-
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...
-
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.
Danny Kalev
Similar Threads
-
Replies: 11
Last Post: 01-09-2008, 09:53 AM
-
By coco861541 in forum Java
Replies: 2
Last Post: 06-26-2005, 07:03 AM
-
By Frank Oquendo in forum .NET
Replies: 116
Last Post: 06-14-2002, 06:04 AM
-
By Dan Fergus in forum .NET
Replies: 3
Last Post: 08-23-2001, 01:39 PM
-
By Courtney Brown in forum XML
Replies: 1
Last Post: 10-06-2000, 11:25 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks