1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Converting Between Binary, Hex, Octal, and Decimal

  1. Jan 25, 2010 #1
    I'm taking a microcontrollers course and need to know how to convert between base 2, 8, 16, and 10. I understand how each base works, however I'm finding it hard to convert between them. What are some good methods to use when converting bases?
     
  2. jcsd
  3. Jan 25, 2010 #2

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    I find the easiest way (by hand) is to just make grids.

    I.e.

    32|16|8|4|2|1

    or

    1048576|65536|4096|256|16|1

    And then fill them out, and add up the numbers.
     
  4. Jan 25, 2010 #3
    I know of that method, but I don't think it would be very effective for large numbers...is it?
     
  5. Jan 25, 2010 #4

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    I suppose there are faster ways...

    Read the number of elements and multiply each element by the base to the power of the place of that element (counting the right most element as place 0). Like, for if we have the number:

    xyztu

    In base b. Then there are 5 elements, so in decimal, it's just:

    x*b^4+y*b^3+z*b^2+t*b^1+u*b^0

    e.g. For Hex:
    13ae3
    1*16^4+3*16^3+11*16^2+15*16^1+3

    I don't really know of any faster ways than this...I'll think about it and see if i can think up something haha.
     
  6. Jan 25, 2010 #5

    vela

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper
    Education Advisor

    Converting between binary and octal or hexadecimal is pretty easy.

    Binary to octal:

    000 = 0
    001 = 1
    010 = 2
    011 = 3
    100 = 4
    101 = 5
    110 = 6
    111 = 7

    For example, octal 436 = binary 100 011 110.

    Similarly, between binary and hexadecimal, you have

    0000 = 0
    0001 = 1
    0010 = 2
    0011 = 3
    0100 = 4
    0101 = 5
    0110 = 6
    0111 = 7
    1000 = 8
    1001 = 9
    1010 = A
    1011 = B
    1100 = C
    1101 = D
    1110 = E
    1111 = F

    Binary 1101 1110 1010 1101 = Hex DEAD

    For converting between decimal and the other bases, all I can suggest is knowing your multiples and powers of 2, 8, and 16. Sometimes converting binary to hex and then to decimal requires less work than going straight from base 2 to base 10, and likewise in the reverse direction.
     
  7. Jan 25, 2010 #6
    Ok, so how about conversions to/from decimal?

    I don't really understand how that method works, care to enlighten me?
     
    Last edited: Jan 25, 2010
  8. Jan 26, 2010 #7

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    It's the same method with the grids, just without the grids. XD

    When you make a grid, it's just:

    b^4|b^3|b^2|b^1|b^0 etc. (in binary then it's 16|8|4|2|1)

    Filling out your numbers there. Without the grid, it's just 0th power, 1st power, 2nd power, etc of your base. All you really need to know is the length of the string of numbers.
     
  9. Jan 26, 2010 #8

    vela

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper
    Education Advisor

    One trick is to use a higher base, like base 256. For instance, suppose you wanted to know what x=hex 6450 is in decimal. Instead of saying

    x = 6*16^3+4*16^2+5*16+0*1 = 25680

    which requires you to calculate what 16^3 is, you could write

    x = (16*6+4)*256^1 + (16*5)*256^0 = 100*256+80 = 25680

    This method saves you a bit of effort because you don't need to know or calculate as many powers of 16 and because converting between hex and decimal is easier for the values from 0 to 255, especially if you know your multiples of 16 up to 16x15.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook