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

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

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



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



실행> booi makeRadixTable.boo

+-------+-------+-------+-------+
|  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
Posted by Scripter
,