Thread: Hexadecimal

  1. #1
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469

    Hexadecimal

    Could someone explain to me how hexadecimal works? eg, how does 0x12 = 18

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay so Hexadecimal is Base 16, which you probably already know. Humans usually use Base 10, or decimal. Hex has the following symbols:

    0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

    And is also prefixed with 0x .
    Your number is 0x12. It is fairly easy to convert to decimal. Take the total number of digits and subtrat one. This will give us one in this case. Then starting left to right, multiply by 16 raised to the x power. Example 0x12.

    ( 1 * 16^1 ) + (2 * 16^0) = 18

    You just decrease in power as you go to the right.
    Perhaps another example

    0x11C - 3 digits , so start with power 2.

    1*(16)^2 + 1*(16)^1 + 12*(16)^0 =

    256 + 16 + 12 = 284


    Hope this cleared it up for you.

  3. #3
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Yup, thanks. Also, is there an easy way to convert numbers into hexadecimal? There must be, I just don't know it.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Basically you just work backwards. Let's take that 284 example in decimal. We want to convert that to Hexadecimal.
    What is the largest power to the 16 that will go into 284. The answer is 2. 16^2 = 256. Because 16^3 = 4096 that is too big. So how many times does 256 go into 284? 1, so our first digit is 1. Let's write that out.

    0x1 is what we have so far. Now we subtract 256 from our original number to obtain 28. 16^1 = 16 so that will go in there. It goes into 28 once, so our next digit is 1 again.

    0x11 so far.

    Then we subtract to get to get 12. Well 16^0 is 1 so that goes into 12 , 12 times, so 12 is C in hex.

    0x11C is our final answer. Any particular one's you don't understand?

  5. #5
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    So there isn't a function you can use?

  6. #6
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Oh yea, and would converting hexadecimals into numbers be the same as vice versa except work backwards on working backwords?

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I'm sure there probably are but you could easily just write your own functions.

    edit: Didn't understand your last question
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Oh, how would you convert hexadecimals into normal numbers?

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If by normal numbers you mean integers, then I just told you how.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    This will print the hexadecimal value of a number.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
       int a;
    
       cout << "Enter number to convert to hex:";
       cin >> a;
       cout << a << " = " << hex << a << " hex" << endl;
       return 0;
    }

  11. #11
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    MrWizard, oh yea.

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    6

    Talking

    Here is the source for a base* to base* converter I made.

    Q

  13. #13
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    That's ok, I got hold of that a long time ago, plus I just wanted to know how hexadecimal worked.

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