1 / 16

Base Conversion

Base Conversion. Number bases. Digital computers usually store integers in base 2 (binary), base 8 (octal), or base 16 (hexadecimal) 266 10 = 100001010 2 = 412 8 = 10A 16. Arbitrary Bases. In base 2, we only need 2 digits, 0,1. In base 16, we need 16 digits

shakti
Download Presentation

Base Conversion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Base Conversion

  2. Number bases • Digital computers usually store integers in base 2 (binary), base 8 (octal), or base 16 (hexadecimal) • 26610 = 1000010102 = 4128 = 10A16

  3. Arbitrary Bases • In base 2, we only need 2 digits, 0,1. • In base 16, we need 16 digits • Use the letters "ABCDEF" to represent the digits from 10 to 15. • With enough digits, integers can be expressed in any base!

  4. Other interesting bases • Base 36. 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ • Base 51. 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-*/=()!@#$%^&_

  5. 4011 (10->16) 57005 (10->16) 1767707668033969 (10->36) 7194 (10->51) 31071116227 (10->51) GQE8RABO5H0JZQGTD709CK (36 -> 51) FAB DEAD HELLOWORLD 2/3 1/2=2/4 This_is_not_a_number Some interesting numbers

  6. Convert one base to another • Convert base 16 to base 10. • Convert any base to base 10. • Convert base 10 to base 16 • Convert base 10 to any base. • Combine Steps 2 and 4.

  7. 1. Base 16 to base 10 • Represent Base 16 numbers as strings. • Digits in Base 16: digits = '0123456789ABCDEF' • digits.find('F') = 15 (15 is base 10 equivalent of digit 'F')

  8. Example • In Base 16: 20A = 20*10 + A • In Base 10: 522 = 32*16 + 10 • If we had a subroutine to convert hex to decimal, this equation would read: • decimal(20A) = decimal(20)*16 + decimal(A)

  9. Code for base 16 to 10 def deciamal(s): d = '0123456789ABCDEF' if len(s) == 1: return d.find(s) else: return decimal(s[:-1])*16 + d.find(s[-1])

  10. 2: Any Base to 10 def decimal(s,d): b = len(d) if len(s) == 1: return d.find(s) else: return decimal(s[:-1],d)*b + d.find(s[-1])

  11. 3. Base 10 to 16 • Think of 522 = a2162 + a116 + a0 • Then a2a1a0 is the hexadecimal representation of 522. • Clearly 522%16 = a0 • Also 522//16 = a216 + a1 = (a2a1) 16

  12. Code decimal to hex def hex(n): d = '0123456789ABCDEF' if n < 16: return d[n] else: return hex(n//16) + d[n%16]

  13. 4. Code decimal to any base def anyBase(n,d): b = len(d) if n < b: return d[n] else: return anyBase(n//b,d) + d[n%b]

  14. 5. Any base to any base def changeBase(s,digits0,digits1): n = decimal(s,digits0) t = anyBase(n,digits1) return t

  15. More usable: def defaultDigits(n): d = '01234567890ABCDEFGHI' + \ 'JKLMNOPQRSTUVWXYZ' + \ 'abcdefghijklmnopqrstuvwxyz'+\ '+-*/!@#$%^&()_' return(d[:n])

  16. 5'. Base to Base def base2base(s,a,b): d0 = defaultDigits(a) d1 = defaultDigits(b) return anyBase(decimal(s,d0),d1)

More Related