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

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

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

  1. ##  Filename: makeRadixTable.jl
  2. ##            Show the radix table with 10-, 2-, 8-, 16-radices.
  3. ##
  4. ##  Execute: julia makeRadixTable.jl
  5. ##
  6. ##      Date:  2013. 3. 5.
  7. ##    Author:  pkim __AT__ scripts ((DOT)) pe ((DOT)) kr
  8. BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. # The function println() is defined already in Julia language.
  10. # function println(s...)
  11. #     if length(s) > 0
  12. #         println(s[1])
  13. #     else
  14. #         println("")
  15. # end
  16. function printUsage()
  17.     println("Usage: julia makeRadixTable.jl")
  18.     println("Show the radix table with 10-, 2-, 8-, 16-radices.")
  19. end
  20. function convertItoA(num, radix)
  21.    isNegative = false
  22.    if num < 0
  23.       isNegative = true
  24.       num = -num
  25.    end
  26.    arr = ""
  27.    q, r = num, 0
  28.    while q >= radix
  29.       # q, r = divmod(q, radix)
  30.       r = mod(q, radix)
  31.       q = div(q, radix)
  32.       arr *= BASE36[r+1]
  33.    end
  34.    arr *= BASE36[q + 1]
  35.    if isNegative
  36.       arr *= "-"
  37.    end
  38.    return reverse(arr)
  39. end
  40. function convertAtoI(srcStr, radix)
  41.     isNegative = false
  42.     ret = 0
  43.     m = length(srcStr)
  44.     val = 0
  45.     c = srcStr[1]
  46.     if c == '-'
  47.         isNegative = true
  48.     elseif c >= '0' && c <= '9'
  49.         ret = int(c) - int('0')
  50.     elseif c >= 'A' && c <= 'Z'
  51.         ret = (int(c) - int('A')) + 10
  52.     elseif c >= 'a' && c <= 'z'
  53.         ret = (int(c) - int('a')) + 10
  54.     end
  55.     if ret >= radix
  56.         println("        Invalid character!")
  57.         return ret
  58.     end
  59.     for i = 2:m
  60.         c = srcStr[i]
  61.         ret *= radix
  62.         if c >= '0' && c <= '9'
  63.             val = int(c) - int('0')
  64.         elseif c >= 'A' && c <= 'Z'
  65.             val = (int(c) - int('A')) + 10
  66.         elseif c >= 'a' && c <= 'z'
  67.             val = (int(c) - int('a')) + 10
  68.         end
  69.         if val >= radix:
  70.             println("        Invalid character!")
  71.             return ret
  72.         end
  73.         ret *= val
  74.     end
  75.     return ret
  76. end
  77. function toWidth(s, width)
  78.     t = s
  79.     if width > length(s)
  80.         t = " "^(width - length(s)) * s
  81.     elseif width < -length(s)
  82.         t = s * " "^(-width - length(s))
  83.     end
  84.     return t
  85. end
  86. function makeTable()
  87.     sbuf = ""
  88.     abuf = ""
  89.     tbuf = ""
  90.     for i = 1:4
  91.         sbuf *= "+-------"
  92.     end
  93.     sbuf *= "+"
  94.     println(sbuf)
  95.     sbuf = "|  Dec"
  96.     sbuf *= "\t|   Bin"
  97.     sbuf *= "\t|  Oct"
  98.     sbuf *= "\t|  Hex  |"
  99.     println(sbuf)
  100.     sbuf = ""
  101.     for i = 1:4
  102.         sbuf *= "+-------"
  103.     end
  104.     sbuf *= "+"
  105.     println(sbuf)
  106.     for i = 0:15
  107.         sbuf = "|   " * toWidth(string(i), 2)
  108.         abuf = convertItoA(i, 2)
  109.         tbuf = "  |  " * toWidth(abuf , 4)
  110.         sbuf *= tbuf
  111.         abuf = convertItoA(i, 8)
  112.         tbuf = " |   " * toWidth(abuf , 2)
  113.         sbuf *= tbuf
  114.         abuf = convertItoA(i, 16)
  115.         tbuf = "  |    "*  toWidth(abuf , -2) * " |"
  116.         sbuf *= tbuf
  117.         println(sbuf)
  118.     end
  119.     sbuf = ""
  120.     for i = 1:4
  121.         sbuf *= "+-------"
  122.     end
  123.     sbuf *= "+"
  124.     println(sbuf)
  125. end
  126. if length(ARGS) > 0 && "-h" == ARGS[1]
  127.     printUsage()
  128.     exit(1)
  129. end
  130. makeTable()



실행> julia makeRadixTable.jl

+-------+-------+-------+-------+
|  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  |
+-------+-------+-------+-------+



 

Posted by Scripter
,