프로그래밍/Julia

7비트 ASCII 코드표 만들기 예제 with Julia

Scripter 2013. 3. 5. 14:06

ASCII(애스키)란 American Standard Code for Information Interchange의 줄임글로서, 영문자에 기초한 문자 인코딩이다.  이 문자 인코딩에는 C0 제어문자(C0 control character)도 포함되어 있다.  ( 참고:  ASCII - Wikipedia, the free encyclopedia )

다음은  7bit ASCII 코드표를 만들어 보여주는 Julia 소스 코드이다. 소스 코드 중에 진법변환에 필요한 함수

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

의 구현도 포함되어 있다.


  1. ##  Filename: makeAsciiTable.jl
  2. ##            Make a table of ascii codes.
  3. ##
  4. ##  Execute: julia makeAsciiTable.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 makeAsciiTable.jl")
  18.     println("Make a table of ascii codes.")
  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. asc = [
  78.     "NUL", "SOH", "STX", "ETX", "EOT",
  79.     "ENQ", "ACK", "BEL", "BS", "HT",
  80.     "LF", "VT", "FF", "CR", "SO",
  81.     "SI", "DLE", "DC1", "DC2", "DC3",
  82.     "DC4", "NAK", "SYN", "ETB", "CAN",
  83.     "EM", "SUB", "ESC", "FS", "GS",
  84.     "RS", "US", "Spc"
  85. ]
  86. control  = [
  87.     "NUL (null)",
  88.     "SOH (start of heading)",
  89.     "STX (start of text)", 
  90.    "ETX (end of text)",
  91.     "EOT (end of transmission)",
  92.     "ENQ (enquiry)",
  93.     "ACK (acknowledge)",
  94.     "BEL (bell)",
  95.     "BS  (backspace)",
  96.     "TAB (horizontal tab)",
  97.     "LF  (line feed, NL new line)",
  98.     "VT  (vertical tab)",
  99.     "FF  (form feed, NP new page)",
  100.     "CR  (carriage return)",
  101.     "SO  (shift out)",
  102.     "SI  (shift in)",
  103.     "DLE (data link escape)",
  104.     "DC1 (device control 1)",
  105.     "DC2 (device control 2)",
  106.     "DC3 (device control 3)",
  107.     "DC4 (device control 4)",
  108.     "NAK (negative acknowledge)",
  109.     "SYN (synchronous idle)",
  110.     "ETB (end of trans. block)",
  111.     "CAN (cancel)",
  112.     "EM  (end of medium)",
  113.     "SUB (substitute, EOF end of file)",
  114.     "ESC (escape)",
  115.     "FS  (file separator)",
  116.     "GS  (group separator)",
  117.     "RS  (record separator)",
  118.     "US  (unit separator)",
  119. ]
  120. function toWidth(s, width)
  121.     t = s
  122.     if width > length(s)
  123.         t = " "^(width - length(s)) * s
  124.     elseif width < -length(s)
  125.         t = s * " "^(-width - length(s))
  126.     end
  127.     return t
  128. end
  129. function makeTable()
  130.     sbuf = ""
  131.     abuf = ""
  132.     tbuf = ""
  133.     sbuf = "    "
  134.     for i = 1:8
  135.         sbuf *= "+----"
  136.     end
  137.     sbuf *= "+"
  138.     println(sbuf)
  139.     sbuf = "    "
  140.     sbuf *= "| 0- "
  141.     sbuf *= "| 1- "
  142.     sbuf *= "| 2- "
  143.     sbuf *= "| 3- "
  144.     sbuf *= "| 4- "
  145.     sbuf *= "| 5- "
  146.     sbuf *= "| 6- "
  147.     sbuf *= "| 7- "
  148.     sbuf *= "|"
  149.     println(sbuf)
  150.     sbuf = "+---"
  151.     for i = 1:8
  152.         sbuf *= "+----"
  153.     end
  154.     sbuf *= "+"
  155.     println(sbuf)
  156.     for i = 0:15
  157.         tbuf = ""
  158.         sbuf = convertItoA(i, 16)
  159.         tbuf *= "| " * sbuf * " "
  160.         for j in 0:7
  161.             if j*16 + i <= 32
  162.                 abuf = "| " * oWidth(asc[j*16 + i + 1], -3)
  163.             elseif j*16 + i == 127
  164.                 abuf = "| " * "DEL"
  165.             else
  166.                 c = char(j*16 + i)
  167.                 abuf = "| " * toWidth(string(c), 2)  * " "
  168.             end
  169.             tbuf *= abuf
  170.         end
  171.         tbuf *= "|"
  172.         println(tbuf)
  173.     end
  174.     sbuf = "+---"
  175.     for i = 1:8
  176.         sbuf *= "+----"
  177.     end
  178.     sbuf *= "+"
  179.     println(sbuf)
  180.     println("")
  181.     for i = 0:15
  182.         tbuf = toWidth(control[i+1], -30) * "  " * toWidth(control[i +16 + 1], -34)
  183.         println(tbuf)
  184.     end
  185. end
  186. if length(ARGS) > 0 && "-h" == ARGS[1]
  187.     printUsage()
  188.     exit(1)
  189. end
  190. makeTable()




실행> julia makeAsciiTable.jl

   
    +----+----+----+----+----+----+----+----+
    | 0- | 1- | 2- | 3- | 4- | 5- | 6- | 7- |
+---+----+----+----+----+----+----+----+----+
| 0 | NUL| DLE| Spc|  0 |  @ |  P |  ` |  p |
| 1 | SOH| DC1|  ! |  1 |  A |  Q |  a |  q |
| 2 | STX| DC2|  " |  2 |  B |  R |  b |  r |
| 3 | ETX| DC3|  # |  3 |  C |  S |  c |  s |
| 4 | EOT| DC4|  $ |  4 |  D |  T |  d |  t |
| 5 | ENQ| NAK|  % |  5 |  E |  U |  e |  u |
| 6 | ACK| SYN|  & |  6 |  F |  V |  f |  v |
| 7 | BEL| ETB|  ' |  7 |  G |  W |  g |  w |
| 8 | BS | CAN|  ( |  8 |  H |  X |  h |  x |
| 9 | HT | EM |  ) |  9 |  I |  Y |  i |  y |
| A | LF | SUB|  * |  : |  J |  Z |  j |  z |
| B | VT | ESC|  + |  ; |  K |  [ |  k |  { |
| C | FF | FS |  , |  < |  L |  \ |  l |  | |
| D | CR | GS |  - |  = |  M |  ] |  m |  } |
| E | SO | RS |  . |  > |  N |  ^ |  n |  ~ |
| F | SI | US |  / |  ? |  O |  _ |  o | DEL|
+---+----+----+----+----+----+----+----+----+

NUL (null)                      DLE (data link escape)
SOH (start of heading)          DC1 (device control 1)
STX (start of text)             DC2 (device control 2)
ETX (end of text)               DC3 (device control 3)
EOT (end of transmission)       DC4 (device control 4)
ENQ (enquiry)                   NAK (negative acknowledge)
ACK (acknowledge)               SYN (synchronous idle)
BEL (bell)                      ETB (end of trans. block)
BS  (backspace)                 CAN (cancel)
TAB (horizontal tab)            EM  (end of medium)
LF  (line feed, NL new line)    SUB (substitute, EOF end of file)
VT  (vertical tab)              ESC (escape)
FF  (form feed, NP new page)    FS  (file separator)
CR  (carriage return)           GS  (group separator)
SO  (shift out)                 RS  (record separator)
SI  (shift in)                  US  (unit separator)