Thread: hexadecimal

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    hexadecimal

    alright so i'm making a program now that takes a decimal number from the user and outputs the number out again but counting in binary, octal, and hexadecimal. I can get binary and octal easily but hexadecimal is hard i don't know how to do it.

    any help?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    switch (num % 16) {
        case 10:
           hex = 'A'
           break;
        case 11:
           hex = 'B'
           break;
        case 12:
           hex = 'C'
           break;
        case 13:
           hex = 'D'
           break;
        case 14:
           hex = 'E'
           break;
        case 15:
           hex = 'F'
           break;
    }
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    ok considering the fact that i'm a noob to this, can you tell me how i would apply that switch statement to what i'm trying to do? lol sorry.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post what you tried.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    asfase

    i haven't really tried anything, i put that code into my program and i don't really know how to code around it so that the user can input 9999 and get the hex number.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I can get binary and octal easily but hexadecimal is hard
    I thought this implied you had some code.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    This is what i have so far. I know it's way more complicated than it has to be and i know i use gotos all over the place but bear with me, i'm a noob.

    Code:
    #include <iostream.h>
    
    main()
    {
    	int choice1, remainder1, remainder2, remainder3, remainder4, remainder5, remainder6, remainder7, remainder8, remainder9, remainder10, remainder11, remainder12, remainder13, remainder14;
    	int base1 = 2;
    	int base2 = 8;
    	int value, value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12, value13, value14;
    	char choice2, hex;
    
    A:	cout << "Welcome to the number converter." << endl;
    D:	cout << "Enter a number between 0 and 9999. " << endl;
    	cin >> value;
    	
    	if ( value > 9999 )
    	{
    		cout << "Please enter a number between 0 and 9999." << endl;
    		cin.clear();
    		cin.ignore();
    		cout << endl;
    		goto D;
    	}
    	else if ( value < 0 )
    	{
    		cout << "That is not a positive number.  Please enter a positive number." << endl;
    		cin.clear();
    		cin.ignore();
    		cout << endl;
    		goto D;
    	}
    	else if ( value > 0 && value < 9999 )
    	{
    		goto E;
    	}
    	else
    	{
    		cout << "Please enter a number Por Favor!" << endl;
    		cin.clear();
    		cin.ignore();
    		cout << endl;
    		goto D;
    	}
    
    E:	cout << "Now, choose what base you would like to convert to." << endl;
    	cout << "   1.) Binary" << endl;
    	cout << "   2.) Octal" << endl;
    	cout << "   3.) Hexadecimal" << endl;
    	cout << "Please enter your choice: ";
    	cin >> choice1;
    
    	if (cin.fail())
    	{
    		cin.clear();
    		cin.ignore();
    		cout << "Don't enter a letter stupid." << endl;
    		goto E;
    	}
    	
    	if (choice1 > 3)
    	{
    		cin.clear();
    		cin.ignore();
    		cout << endl << "C'mon idiot it gives you the choices now obey!" << endl;
    		goto E;
    	}
    
    	if (choice1 < 1)
    	{
    		cin.clear();
    		cin.ignore();
    		cout << endl << "C'mon idiot it gives you the choices now obey!" << endl;
    		goto E;
    	}
    
    	switch ( choice1 )
    	{
    	case 1:
    		value1 = value / base1;
    		remainder1 = value % 2;
    		value2 = value1 / base1;
    		remainder2 = value1 % 2;
    		value3 = value2 / base1;
    		remainder3 = value2 % 2;
    		value4 = value3 / base1;
    		remainder4 = value3 % 2;
    		value5 = value4 / base1;
    		remainder5 = value4 % 2;
    		value6 = value5 / base1;
    		remainder6 = value5 % 2;
    		value7 = value6 / base1;
    		remainder7 = value6 % 2;
    		value8 = value7 / base1;
    		remainder8 = value7 % 2;
    		value9 = value8 / base1;
    		remainder9 = value8 % 2;
    		value10 = value9 / base1;
    		remainder10 = value9 % 2;
    		value11 = value10 / base1;
    		remainder11 = value10 % 2;
    		value12 = value11 / base1;
    		remainder12 = value11 % 2;
    		value13 = value12 / base1;
    		remainder13 = value12 % 2;
    		value14 = value13 / base1;
    		remainder14 = value13 % 2;
    		cout << remainder14 << remainder13 << remainder12 << remainder11 << remainder10 << remainder9 << remainder8 << remainder7;
    		cout << remainder6 << remainder5 << remainder4 << remainder3 << remainder2 << remainder1;
    		cout << endl << endl << "Exclude the zeros to the left of the first 1 digit." << endl;
    		break;
    	case 2:
    		value1 = value / base2;
    		remainder1 = value % 8;
    		value2 = value1 / base2;
    		remainder2 = value1 % 8;
    		value3 = value2 / base2;
    		remainder3 = value2 % 8;
    		value4 = value3 / base2;
    		remainder4 = value3 % 8;
    		value5 = value4 / base2;
    		remainder5 = value4 % 8;
    		value6 = value5 / base2;
    		remainder6 = value5 % 8;
    		value7 = value6 / base2;
    		remainder7 = value6 % 8;
    		value8 = value7 / base2;
    		remainder8 = value7 % 8;
    		value9 = value8 / base2;
    		remainder9 = value8 % 8;
    		value10 = value9 / base2;
    		remainder10 = value9 % 8;
    		value11 = value10 / base2;
    		remainder11 = value10 % 8;
    		value12 = value11 / base2;
    		remainder12 = value11 % 8;
    		value13 = value12 / base2;
    		remainder13 = value12 % 8;
    		cout << remainder13 << remainder12 << remainder11 << remainder10 << remainder9 << remainder8 << remainder7;
    		cout << remainder6 << remainder5 << remainder4 << remainder3 << remainder2 << remainder1;
    		cout << endl << endl << "Exclude the zeros to the left of the first whole number digit." << endl;
    		break;
    	case 3:
    		break;
    	
    	
    
    C:	cout << endl << "Would you like to try again? y or n ";
    	cin >> choice2;
    
    	if ( choice2 == 'y' )
    	{
    		cin.clear();
    		cin.ignore();
    		cout << endl << endl;
    		goto A;
    	}
    	
    	if ( choice2 == 'n' )
    	{
    		cout << endl << endl;
    		goto B;
    	}
    
    	if ( choice2 != 'n'||'y' )
    	{
    		cout << "That ain't right.  try again." << endl;
    		cin.clear();
    		cout << endl << endl;
    		goto C;
    	}
    
    B:  cout << "Thanks for.....playing?" << endl;
    
    	return 0;
    }

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    This is fun:
    Code:
    // For class, eh? Removed.
    Don't worry about the string stream if you don't know it. You can do this with plain ol' strings as well.
    Last edited by SlyMaelstrom; 02-13-2006 at 09:41 AM.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    this is also something i don't understand.

    LOL i feel dwarfed by you guys.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ah... don't feel dwarfed by me. Salem, yes. Me, no.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    grrrr with hexadecimal.

    this is for a c++ class btw and this is due like....tomorrow.

    crap XD

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I hope you don't expect to get anything better than an F on that program.

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You should really consider using a loop since you can really see an iterative structure in that code.
    Code:
    while(Number > 0)
    {
      Digit = Number % Base;
      PrependToResult(Digit);
      Number /= Base;
    }
    To convert the number to a hex character:
    Code:
    char NumberToHex(int Number)
    {
      return (Number < 10) ? ('0' + Number) : ('A' + Number - 10);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, for oct and hex are you allowed to do this:
    Code:
    int main()
    {
        int number;
        cout << "Enter a number: ";
        cin >> number;
    
        cout << "Oct: " << oct << number << endl;
        cout << "Hex: " << hex << number << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I nominate hk_mp5pdw the king of standard C++ keywords and functions.

    ...and no, I don't think he can do that one.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-02-2007, 05:55 AM
  2. hexadecimal to ascii
    By jpablo in forum C Programming
    Replies: 2
    Last Post: 04-12-2006, 05:28 PM
  3. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 03:55 PM
  4. hexadecimal
    By pierremk in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2002, 08:05 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM