프로그래밍/Io

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

Scripter 2008. 5. 2. 00:10

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

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

        convertAtoI(String, radix)
        convertItoA(long, radix)

의 구현도 포함되어 있다.


  1. /*
  2.  *  Filename: makeAsciiTable.io
  3.  *            Make a table of ascii codes.
  4.  *
  5.  *  Execute: io makeAsciiTable.io
  6.  *
  7.  *      Date:  2008/05/02
  8.  *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  9.  */
  10. printUsage := method(
  11.     "Usage: io makeAsciiTable.io" println
  12.     "Make a table of ascii codes." println
  13. )
  14. convertItoA := method(num, radix,
  15.     BASE36 := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  16.     q := 0
  17.     r := 0
  18.     isNegative := false
  19.     if (num < 0) then (
  20.         isNegative = true
  21.         num = -num
  22.     )
  23.     arr := ""
  24.     q = num
  25.     r = 0
  26.     while (q >= radix,
  27.         r = q % radix
  28.         q = (q / radix) floor
  29.         arr = arr .. (BASE36 slice(r, r + 1))
  30.      )
  31.      arr =  arr .. (BASE36 slice(q, q + 1))
  32.      if (isNegative) then (
  33.          arr = arr .. "-"
  34.      )
  35.      n := arr size
  36.      ret := ""
  37.      for (i, 0, n - 1,
  38.          ret = ret .. arr slice(n - i - 1, n - i)
  39.      )
  40.      return ret
  41. )
  42. convertAtoI := method(srcStr, radix,
  43.     BASE36 := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  44.     isNegative := false
  45.     ret := 0
  46.     len := srcStr size
  47.     c := ""
  48.     i := 0
  49.     val := 0
  50.     c = srcStr at(0)
  51.     if (c == "-") then (
  52.         isNegative = true
  53.     ) elseif (c >= "0" and c <= "9") then (
  54.         ret = (c product) - ("0" product)
  55.     ) elseif (c >= "A" and c <= "Z") then (
  56.         ret = (c product) - ("A" product) + 10
  57.     ) elseif (c >= "a" and  <= "z") then (
  58.         ret = (c product) - ("a" product) + 10
  59.     )
  60.     if (ret >= adix) then (
  61.         "        Invalid character!" println
  62.         return ret
  63.     )
  64.     for (i, 1,  len - 1,
  65.         c = srcStr[i]
  66.         ret = ret * radix
  67.         if (c >= "0" and c <= "9") then (
  68.             val = c product - "0" product
  69.         ) elseif (c >= 'A' and c <= 'Z') then (
  70.             val = c product - "A" product + 10
  71.         ) elseif (c >= 'a' and c <= 'z') then (
  72.             val = c product - "a" product + 10
  73.         )
  74.         if (val >= (long) radix) then (
  75.             "        Invalid character!" println
  76.             return ret
  77.         )
  78.         ret = ret + val
  79.     )
  80.     return ret
  81. )
  82. makeTable := method(
  83.     asc := list( "NUL", "SOH", "STX", "ETX", "EOT" )
  84.     asc append( "ENQ", "ACK", "BEL", "BS", "HT" )
  85.     asc append( "LF", "VT", "FF", "CR", "SO" )
  86.     asc append( "SI", "DLE", "DC1", "DC2", "DC3" )
  87.     asc append( "DC4", "NAK", "SYN", "ETB", "CAN" )
  88.     asc append( "EM", "SUB", "ESC", "FS", "GS" )
  89.     asc append( "RS", "US", "Spc" )
  90.     control := list( "NUL (null)" )
  91.     control append( "SOH (start of heading)" )
  92.     control append( "STX (start of text)" )
  93.     control append( "ETX (end of text)" )
  94.     control append( "EOT (end of transmission)" )
  95.     control append( "ENQ (enquiry)" )
  96.     control append( "ACK (acknowledge)" )
  97.     control append( "BEL (bell)" )
  98.     control append( "BS  (backspace)" )
  99.     control append( "TAB (horizontal tab)" )
  100.     control append( "LF  (line feed, NL new line)" )
  101.     control append( "VT  (vertical tab)" )
  102.     control append( "FF  (form feed, NP new page)" )
  103.     control append( "CR  (carriage return)" )
  104.     control append( "SO  (shift out)" )
  105.     control append( "SI  (shift in)" )
  106.     control append( "DLE (data link escape)" )
  107.     control append( "DC1 (device control 1)" )
  108.     control append( "DC2 (device control 2)" )
  109.     control append( "DC3 (device control 3)" )
  110.     control append( "DC4 (device control 4)" )
  111.     control append( "NAK (negative acknowledge)" )
  112.     control append( "SYN (synchronous idle)" )
  113.     control append( "ETB (end of trans. block)" )
  114.     control append( "CAN (cancel)" )
  115.     control append( "EM  (end of medium)" )
  116.     control append( "SUB (substitute, EOF end of file)" )
  117.     control append( "ESC (escape)" )
  118.     control append( "FS  (file separator)" )
  119.     control append( "GS  (group separator)" )
  120.     control append( "RS  (record separator)" )
  121.     control append( "US  (unit separator)" )
  122.     sbuf := ""
  123.     abuf := ""
  124.     tbuf := ""
  125.     i := 0
  126.     j := 0
  127.     c := ""
  128.     "    " print
  129.     for (i, 0, 7,
  130.         "+----" print
  131.     )
  132.     "+" print
  133.     "" println
  134.     "    " print
  135.     "| 0- " print
  136.     "| 1- " print
  137.     "| 2- " print
  138.     "| 3- " print
  139.     "| 4- " print
  140.     "| 5- " print
  141.     "| 6- " print
  142.     "| 7- " print
  143.     "|" print
  144.     "" println
  145.     "+---" print
  146.     for (i, 0, 7,
  147.         "+----" print
  148.     )
  149.     "+" print
  150.     "" println
  151.     tmpS := ""
  152.     for (i, 0, 15,
  153.         tbuf = ""
  154.         sbuf = convertItoA(i, 16)
  155.         tbuf = tbuf .. "| " .. sbuf .. " "
  156.         for (j, 0, 7,
  157.             if (j*16 + i <= 32) then (
  158.                 tmpS = asc at(j*16 + i)
  159.                 if (tmpS size < 3) then ( tmpS = tmpS .. (" " repeated(3 - (tmpS size)) ) )
  160.                 abuf = "| " .. tmpS
  161.             ) elseif (j*16 + i == 127) then (
  162.                 tmpS = "DEL"
  163.                 if (tmpS size < 3) then ( tmpS = tmpS .. (" " repeated(3 - (tmpS size)) ) )
  164.                 abuf = "| " .. tmpS
  165.             ) else (
  166.                 c := Sequence clone
  167.                 c append(j*16 + i)
  168.                 tmpS = c asString
  169.                 if (tmpS size < 2) then ( tmpS = (" " repeated(2 - (tmpS size)) .. tmpS ) )
  170.                 abuf = "| " .. tmpS .. " "
  171.             )
  172.             tbuf = tbuf .. abuf
  173.         )
  174.         tbuf = tbuf .. "|"
  175.         tbuf println
  176.     )
  177.     "+---" print
  178.     for (i, 0, 7,
  179.         "+----" print
  180.     )
  181.     "+" print
  182.     "" println
  183.     "" println
  184.     for (i, 0, 15,
  185.         // sbuf = String.format("%-30s",  control at(i)
  186.         // tbuf = String.format("  %-34s",  control at(i+16))
  187.         tmpS = control at(i)
  188.         if (tmpS size < 30) then ( tmpS = tmpS .. (" " repeated(30 - (tmpS size)) ) )
  189.         sbuf = tmpS
  190.         tmpS = control at(i+16)
  191.         if (tmpS size < 34) then ( tmpS = tmpS .. (" " repeated(34 - (tmpS size)) ) )
  192.         tbuf = tmpS
  193.         sbuf print
  194.         tbuf println
  195.     )
  196. )
  197. if (args size > 1 and "-h" == args at(1)) then (
  198.     printUsage()
  199.     exit(1)
  200. )
  201. makeTable()




실행> io makeAsciiTable.io

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




Creative Commons License

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