Converting Integers to Hex...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Converting Integers to Hex...

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Posts
    1

    Question 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

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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

  3. #3
    Join Date
    May 2005
    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. #4
    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. #5
    Join Date
    Nov 2003
    Posts
    4,118
    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

  6. #6
    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. #7
    Join Date
    Nov 2003
    Posts
    4,118
    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

  1. Converting input strings to integers
    By Dave in forum .NET
    Replies: 11
    Last Post: 01-09-2008, 10:53 AM
  2. Loop converting to hex - please help
    By coco861541 in forum Java
    Replies: 2
    Last Post: 06-26-2005, 08:03 AM
  3. Converting projects to VB.NET
    By Frank Oquendo in forum .NET
    Replies: 116
    Last Post: 06-14-2002, 07:04 AM
  4. Need to Convert a Hex string to a Hex number
    By Dan Fergus in forum .NET
    Replies: 3
    Last Post: 08-23-2001, 02:39 PM
  5. Replies: 1
    Last Post: 10-06-2000, 12:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center