컴퓨터 프로그래밍에서 꼭 알아두어야 할 주요 진법은 당연히 10진법, 2진법, 8진법, 16진법이다.
다음은  0 에서 15 까지의 정수를 10진법, 2진법, 8진법, 16진법의 표로 만들어 보여주는 Python 소스 코드이다. 진법 변환에 필요한 함수

        convertAtoI(string, radix)
        convertItoA(number, radix)

를 Python 코드로 자체 작성하여 사용하였다.

(아래의 소스는 Jython이나 IronPython에서도 수정없이 그대로 실행된다.)



  1. #  Filename: makeRadixTable.py
  2. #            Show the radix table with 10-, 2-, 8-, 16-radices.
  3. #
  4. #  Execute: python makeRadixTable.py
  5. #
  6. #      Date:  2008/03/28
  7. #    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  8. import sys
  9. BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  10. def println(s=None):
  11.     if s == None:
  12.         print
  13.     else:
  14.         print(s)
  15. def printUsage():
  16.     println("Usage: python makeRadixTable.py")
  17.     println("Show the radix table with 10-, 2-, 8-, 16-radices.")
  18. def convertItoA(num, radix):
  19.     isNegative = False
  20.     if num < 0:
  21.         isNegative = True
  22.         num = -num
  23.     arr = ""
  24.     q = num
  25.     r = 0
  26.     while q >= radix:
  27.         r = q % radix
  28.         q = q / radix
  29.         arr += BASE36[r]
  30.     arr += BASE36[q]
  31.     if isNegative:
  32.         arr += "-"
  33.     n = len(arr)
  34.     ret = ""
  35.     for i in range(0, n):
  36.         ret += arr[n - i - 1]
  37.     return ret
  38. def convertAtoI(srcStr, radix):
  39.     isNegative = False
  40.     ret = 0
  41.     m = len(srcStr)
  42.     val = 0
  43.     c = srcStr[0]
  44.     if c == '-':
  45.         isNegative = True
  46.     elif c >= '0' and c <= '9':
  47.         ret = ord(c) - ord('0')
  48.     elif c >= 'A' and c <= 'Z':
  49.         ret = (ord(c) - ord('A')) + 10
  50.     elif c >= 'a' and c <= 'z':
  51.         ret = (ord(c) - ord('a')) + 10
  52.     if ret >= radix:
  53.         println("        Invalid character!")
  54.         return ret
  55.     for i in range(1, m):
  56.         c = srcStr[i]
  57.         ret *= radix
  58.         if c >= '0' and c <= '9':
  59.             val = ord(c) - ord('0')
  60.         elif c >= 'A' and c <= 'Z':
  61.             val = (ord(c) - ord('A')) + 10
  62.         elif c >= 'a' and c <= 'z':
  63.             val = (ord(c) - ord('a')) + 10
  64.         if val >= radix:
  65.             println("        Invalid character!")
  66.             return ret
  67.         ret += val
  68.     return ret
  69. def makeTable():
  70.     sbuf = ""
  71.     abuf = ""
  72.     tbuf = ""
  73.     for i in range(0, 4):
  74.         sbuf += "+-------"
  75.     sbuf += "+"
  76.     println(sbuf)
  77.     sbuf = "|  Dec"
  78.     sbuf += "\t|   Bin"
  79.     sbuf += "\t|  Oct"
  80.     sbuf += "\t|  Hex  |"
  81.     println(sbuf)
  82.     sbuf = ""
  83.     for i in range(0, 4):
  84.         sbuf += "+-------"
  85.     sbuf += "+"
  86.     println(sbuf)
  87.     for i in range(0, 16):
  88.         sbuf = "|   %2d" % i
  89.         abuf = convertItoA(i, 2)
  90.         tbuf = "\t|  %4s" % abuf
  91.         sbuf += tbuf
  92.         abuf = convertItoA(i, 8)
  93.         tbuf = "\t|   %2s" % abuf
  94.         sbuf += tbuf
  95.         abuf = convertItoA(i, 16)
  96.         tbuf = "\t|    %-2s |" % abuf
  97.         sbuf += tbuf
  98.         println(sbuf)
  99.     sbuf = ""
  100.     for i in range(0, 4):
  101.         sbuf += "+-------"
  102.     sbuf += "+"
  103.     println(sbuf)
  104. if len(sys.argv) > 1 and "-h" == sys.argv[1]:
  105.     printUsage()
  106.     sys.exit(1)
  107. makeTable()



실행> python makeRadixTable.py

+-------+-------+-------+-------+
|  Dec  |   Bin |  Oct  |  Hex  |
+-------+-------+-------+-------+
|    0  |     0 |    0  |    0  |
|    1  |     1 |    1  |    1  |
|    2  |    10 |    2  |    2  |
|    3  |    11 |    3  |    3  |
|    4  |   100 |    4  |    4  |
|    5  |   101 |    5  |    5  |
|    6  |   110 |    6  |    6  |
|    7  |   111 |    7  |    7  |
|    8  |  1000 |   10  |    8  |
|    9  |  1001 |   11  |    9  |
|   10  |  1010 |   12  |    A  |
|   11  |  1011 |   13  |    B  |
|   12  |  1100 |   14  |    C  |
|   13  |  1101 |   15  |    D  |
|   14  |  1110 |   16  |    E  |
|   15  |  1111 |   17  |    F  |
+-------+-------+-------+-------+




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,