다음은  대화형 모드(interactive mode)에서 진법 변환(radix conversion)하는 Lua 소스 코드이다.
메뉴는 주메뉴 Command: (S)et radix, (A)bout, (Q)uit or E(x)it
와 부메뉴 SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
로 구성되어 있으며, 진법 변환의 핵심은 Lua 의 빌트인 함수 tonumber(string, radix)와 소스 코드에 자체 작성된 함수 itoa(number, radix)의 사용이다.

         val = tonumber(s, srcRdx)
         ret = itoa(val, destRdx)

지원되는 진법은 2진법에서 36진법 까지이다.



  1. --  Filename: convertRadix.lua
  2. --            Convert radix in a interactive mode.
  3. --
  4. --   Execute: lua convertRadix.lua
  5. --
  6. --      Date:  2008/03/26
  7. --    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  8. function println(s)
  9.     if s ~= nil then
  10.         print(s)
  11.     else
  12.         print("")
  13.     end
  14. end
  15. function printUsage()
  16.     println("Usage: lua convertRadix.lua")
  17.     println("Convert radix in a interactive mode, where the maximum radix is 36.")
  18. end
  19. function printAbout()
  20.     println("    About: Convert radix in a interactive mode.")
  21. end
  22. function printMainMenu()
  23.     println("  Command: (S)et radix, (A)bout, (Q)uit or E(x)it")
  24. end
  25. function printMainPrompt()
  26.     io.write("  Prompt> ")
  27. end
  28. function printSubMenu(srcRadix, destRadix)
  29.     println("    Convert Radix_" .. srcRadix .. " to Radix_" .. destRadix)
  30.     println("    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit")
  31. end
  32. function printSubPrompt()
  33.     io.write("    Input Value>> ")
  34. end
  35. local BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  36. function itoa(num, radix)
  37.    local isNegative = false
  38.    if num < 0 then
  39.       isNegative = true
  40.       num = -num
  41.    end
  42.    local arr = {}
  43.    local q, r = num, 0
  44.    while q >= radix do
  45.       r = math.fmod(q, radix)
  46.       q = math.floor(q / radix)
  47.       table.insert(arr, string.sub(BASE36, r+1, r+1))
  48.    end
  49.    table.insert(arr, string.sub(BASE36, q+1, q+1))
  50.    if isNegative then
  51.       table.insert(arr, "-")
  52.    end
  53.    local ret = ""
  54.    for i = #arr, 1, -1 do
  55.        ret = ret .. arr[i]
  56.    end
  57.    return ret
  58. end
  59. function convertRadix(s, srcRdx, destRdx)
  60.     local val = tonumber(s, srcRdx)
  61.     local ret = itoa(val, destRdx)
  62.     return string.upper(ret)
  63. end
  64. function doConvert(srcRadix, destRadix)
  65.     local line = ""
  66.     local cmd = ""
  67.     local srcStr = ""
  68.     local destStr = ""
  69.     printSubMenu(srcRadix, destRadix)
  70.     while true do
  71.         printSubPrompt()
  72.         cmd = io.read()
  73.         while string.len(cmd) < 1 do
  74.             cmd = io.read()
  75.         end
  76.         if "main()" == cmd then
  77.             return
  78.         elseif "exit()" == cmd or "quit()" == cmd then
  79.             os.exit(0)
  80.         end
  81.         local flagContinue = true
  82.         if tonumber(cmd, srcRadix) == nil then
  83.             flagContinue = false
  84.         end
  85.         if flagContinue then
  86.             srcStr = cmd
  87.             destStr = convertRadix(srcStr, srcRadix, destRadix)
  88.             println("        ( " .. srcStr .. " )_" .. srcRadix .. "   --->   ( " .. destStr .. " )_" .. destRadix)
  89.             println("")
  90.         end
  91.     end
  92. end
  93. function doStart()
  94.     local line = ""
  95.     local cmd = ""
  96.     local srcRadix = 10
  97.     local destRadix = 10
  98.     local srcStr = ""
  99.     local destStr = ""
  100.     local onlyOnce = true
  101.     while true do
  102.         println()
  103.         if onlyOnce then
  104.             println("  The supported maximum radix is 36.")
  105.             onlyOnce = false
  106.         end
  107.         printMainMenu()
  108.         printMainPrompt()
  109.         cmd = io.read()
  110.         while string.len(cmd) < 1 do
  111.             cmd = io.read()
  112.         end
  113.         if string.find("qQxX", cmd) ~= nil and string.len(cmd) == 1 then
  114.             os.exit(0)
  115.         elseif string.find("aA", cmd) ~= nil and string.len(cmd) == 1 then
  116.             printAbout()
  117.         elseif string.find("sS", cmd) ~= nil and string.len(cmd) == 1 then
  118.             io.write("  Input the source and target radices (say, 16 2): ")
  119.             line = io.read()
  120.             while string.len(line) < 1 do
  121.                 line = io.read()
  122.             end
  123.             st = {}
  124.             for w in string.gmatch(line, "[0-9]*") do
  125.                 if string.len(w) > 0 then
  126.                     table.insert(st, w)
  127.                 end
  128.             end
  129.             while #st < 2 do
  130.                 io.write("  Input the source and target radices (say, 16 2): ")
  131.                 line = io.read()
  132.                while string.len(line) < 1 do
  133.                     line = io.read()
  134.                 end
  135.                 st = {}
  136.                 for w in string.gmatch(line, "%a+") do
  137.                     if string.len(w) > 0 then
  138.                         table.insert(st, w)
  139.                     end
  140.                 end
  141.             end
  142.             println("")
  143.             srcRadix = tonumber(st[1])
  144.             destRadix = tonumber(st[2])
  145.             doConvert(srcRadix, destRadix)
  146.         end
  147.     end
  148. end
  149. -- Begin here
  150. if #arg > 0 and "-h" == arg[1] then
  151.     printUsage()
  152.     os.exit(1)
  153. end
  154. doStart()




실행> lua convertRadix.lua

  The supported maximum radix is 36.
  Command: (S)et radix, (A)bout, (Q)uit or E(x)it
  Prompt> s
  Input the source and target radices (say, 16 2): 16 2

    Convert Radix_16 to Radix_2
    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
    Input Value>> 7F
        ( 7F )_16   --->   ( 1111111 )_2

    Input Value>> main()

  Command: (S)et radix, (A)bout, (Q)uit or E(x)it
  Prompt> S
  Input the source and target radices (say, 16 2): 2 16

    Convert Radix_2 to Radix_16
    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
    Input Value>> 1111111
        ( 1111111 )_2   --->   ( 7F )_16

    Input Value>> exit()





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

Posted by Scripter
,