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

        atoi(string, radix)
        itoa(number, radix)

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

(아래의 소스는 Python 소스를 F# 소스로 일대일 변환 수정한 것이라서, F# 언어의 명령형 언어 특징을 위주로 작성되어 있다.)

  1. (*
  2.  *  Filename: MakeRadixTable.fs
  3.  *            Show the radix table with 10-, 2-, 8-, 16-radices.
  4.  *
  5.  *  Compile: fsc MakeRadixTable.fs
  6.  *  Execute: MakeRadixTable
  7.  *
  8.  *      Date:  2010/07/15
  9.  *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  10.  *)
  11. #light
  12. exception RuntimeError of string
  13. exception ValueError of string
  14. let BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  15. let println s =
  16.     printfn "%O" s
  17. let print s =
  18.     printf "%O" s
  19. let printUsage dummy =
  20.     println "Usage: MakeRadixTable"
  21.     println "Show the radix table with 10-, 2-, 8-, 16-radices."
  22. let itoa (num : int, radix : int) =
  23.    let mutable isNegative = false
  24.    let mutable numx = num
  25.    if num < 0 then
  26.       isNegative <- true
  27.       numx <- (-num)
  28.    let mutable arr = [  ]
  29.    let mutable q = numx
  30.    let mutable r = 0
  31.    while (q >= radix) do
  32.        r <- int (q % radix)
  33.        q <- int (q / radix)
  34.        arr <- List.append arr [ sprintf "%O" (BASE36.[r]) ]
  35.    arr <- List.append arr [ sprintf "%O" (BASE36.[q]) ]
  36.    if isNegative then
  37.       arr <- List.append arr [ "-" ]
  38.    arr <- List.rev arr
  39.    System.String.Join("", List.toArray arr)
  40. let atoi (s : string, radix : int) : int =
  41.     let mutable ret = 0
  42.     let mutable isNegative = false
  43.     let len = s.Length
  44.     let mutable valx = 0
  45.     let mutable c = s.[0]
  46.     if c = '-' then
  47.         isNegative <- true
  48.     elif (c >= '0' && c <= '9') then
  49.         ret <- (int c) - (int '0')
  50.     elif (c >= 'A' && c <= 'Z') then
  51.         ret <- int c - int 'A' + 10
  52.     elif (c >= 'a' && c <= 'z') then
  53.         ret <- int c - int 'a' + 10
  54.     if (ret >= radix) then
  55.         printfn "    Error: Can not read \"%s\" (as radix %d): %O is an invalid character!" s radix ret
  56.     for i = 1 to len - 1 do
  57.         c <- s.[i]
  58.         ret <- ret*radix
  59.         if (c >= '0' && c <= '9') then
  60.             valx <- int c - int '0'
  61.         elif (c >= 'A' && c <= 'Z') then
  62.             valx <- int c - int 'A' + 10
  63.         elif (c >= 'a' && c <= 'z') then
  64.             valx <- int c - int 'a' + 10
  65.         if (valx >= radix) then
  66.             printfn "    Error: Can not read \"%s\" (as radix %d): %O is an invalid character!" s radix c
  67.         ret <- ret + valx
  68.     if (isNegative) then
  69.         ret <- (-ret )
  70.     ret
  71. let makeTable dummy =
  72.     let mutable sbuf = ""
  73.     let mutable abuf = ""
  74.     let mutable tbuf = ""
  75.     for i in 0..(4-1) do
  76.         sbuf <- sbuf + "+-------"
  77.     sbuf <- sbuf + "+"
  78.     println(sbuf)
  79.     sbuf <- "|  Dec"
  80.     sbuf <- sbuf +  "\t|   Bin"
  81.     sbuf <- sbuf +  "\t|  Oct"
  82.     sbuf <- sbuf +  "\t|  Hex  |"
  83.     println(sbuf)
  84.     sbuf <- ""
  85.     for i in 0..(4-1) do
  86.         sbuf <- sbuf +  "+-------"
  87.     sbuf <- sbuf +  "+"
  88.     println(sbuf)
  89.     for i in 0..(16-1) do
  90.         sbuf <- sprintf "|   %2d" i
  91.         abuf <- itoa(i, 2)
  92.         tbuf <- sprintf "\t|  %4s" abuf
  93.         sbuf <- sbuf +  tbuf
  94.         abuf <- itoa(i, 8)
  95.         tbuf <- sprintf "\t|   %2s" abuf
  96.         sbuf <- sbuf + tbuf
  97.         abuf <- itoa(i, 16)
  98.         tbuf <- sprintf "\t|    %-2s |"  abuf
  99.         sbuf <- sbuf +  tbuf
  100.         println(sbuf)
  101.     sbuf <- ""
  102.     for i in 0..(4-1) do
  103.         sbuf <- sbuf +  "+-------"
  104.     sbuf <- sbuf +  "+"
  105.     println(sbuf)
  106. // Begin here
  107. let cmdArgs = System.Environment.GetCommandLineArgs()
  108. if (Array.length cmdArgs > 1 && "-h" = cmdArgs.[1]) then
  109.     printUsage 0
  110.     exit(1)
  111. makeTable 0


컴파일> fsc MakeRadixTable.fs

실행> MakeRadixTable

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