Page 38 - Programming With Python 3
P. 38

An Introduction to STEM Programming with Python — 2019-09-03a                               Page 25
            Chapter 2 — Numbering Systems

            Convert 63987 to Hexadecimal:

             Free                                                          __F3
                                            63987 / 16 = 3999 r 3
                                                                           ___3
                                            3999 / 16 = 249 r 15
                                            249 / 16 = 15 r 9

                                                                           F9F3
                                            15 / 16 = 0 r 15               _9F3
             eBook
            In Python we can convert to base 16 using code just like we did to convert to binary. We find the
            remainder from 26 and divide by 26. Reading the remainders from bottom to top.

               1|  n=63987
               2|  r = n % 16
               3|  n = n //16
             Edition
               4|  print(n,r)
               5|  r = n % 16
               6|  n = n //16
               7|  print(n,r)
               8|  r = n % 16
            Please support this work at
               9|  n = n //16
             10|  print(n,r)
             11|  r = n % 16
             12|  n = n //16
                                  http://syw2l.org
             13|  print(n,r)

                    3999 3
                    249 15
                    15 9
                    0 15                                                       Free


            This can also be written using the while loop to simplify.

               1|  n=63987
               2|  while(n):
               3|      r = n % 16                                  eBook
               4|      n = n //16
               5|      print(n,r)

                                                                Edition
            Hexadecimal to Decimal (Positional Method)



            We can also convert from Hexadecimal to decimal by using the powers of 16.



            Copyright 2019 — James M. Reneau Ph.D. — http://www.syw2l.org — This work is licensed
            under a Creative Commons Attribution-ShareAlike 4.0 International License.
   33   34   35   36   37   38   39   40   41   42   43