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

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

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

의 구현도 포함되어 있다. 소스코드의 170째 줄에
 
        c = cast(char, j*16 + i)    // casting from int to char

로 작성된 것은 식 j*16 + i 로 계산된 int 값을 char 타입으로 캐스팅하는 장면이다. 참고로 C/C++/Java 언어라면
 
        c = (char) (j*16 + i)     // casting from int to char

로 작성되었울 것이다. 일반적으로 Boo 언어에서 캐스팅하는 구문은
 
           변수명 = cast(새타입, 식)
이다.



  1. #  Filename: makeAsciiTable.boo
  2. #            Make a table of ascii codes.
  3. #
  4. #  Execute: booi makeAsciiTable.boo
  5. #
  6. #      Date:  2009/04/04
  7. #    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  8. import System
  9. BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  10. def println(s as string):
  11.     print(s)
  12. def printUsage():
  13.     println("Usage: booi makeAsciiTable.boo")
  14.     println("Make a table of ascii codes.")
  15. def convertItoA(num as long, radix as int) as string:
  16.     isNegative = false
  17.     if num < 0:
  18.         isNegative = true
  19.         num = -num
  20.     arr as string = ""
  21.     q as long = num
  22.     r as long = 0
  23.     while q >= radix:
  24.         r = q % radix
  25.         q = q / radix
  26.         arr += BASE36[r]
  27.     arr += BASE36[q]
  28.     if isNegative:
  29.         arr += "-"
  30.     s as string = ""
  31.     n = len(arr)
  32.     for i as int in range(n):
  33.         s += arr[n - 1 - i]
  34.     return s
  35. def toAscii(s as String) as int:
  36.     objAscii as System.Text.ASCIIEncoding = System.Text.ASCIIEncoding()
  37.     val as System.Byte = objAscii.GetBytes(s)[0]
  38.     return Convert.ToInt32(val)
  39. def convertAtoI(srcStr as string, radix as int) as long:
  40.     isNegative = false
  41.     ret as long = 0L
  42.     m = len(srcStr)
  43.     val = 0
  44.     c = srcStr[0:1]
  45.     if toAscii(c) == toAscii('-'):
  46.         isNegative = true
  47.     elif toAscii(c) >= toAscii('0') and toAscii(c) <= toAscii('9'):
  48.         ret = toAscii(c) - toAscii('0')
  49.     elif toAscii(c) >= toAscii('A') and toAscii(c) <= toAscii('Z'):
  50.         ret = (toAscii(c) - toAscii('A')) + 10
  51.     elif toAscii(c) >= toAscii('a') and toAscii(c) <= toAscii('z'):
  52.         ret = (toAscii(c) - toAscii('a')) + 10
  53.     if ret >= radix:
  54.         raise Exception("        invalid character!")
  55.         return ret
  56.     for i in range(1, m):
  57.         c = srcStr[i:i+1]
  58.         ret *= radix
  59.         if toAscii(c) >= char('0') and toAscii(c) <= char('9'):
  60.             val = toAscii(c) - toAscii('0')
  61.         elif toAscii(c) >= char('A') and toAscii(c) <= char('Z'):
  62.             val = (toAscii(c) - toAscii('A')) + 10
  63.         elif toAscii(c) >= char('a') and toAscii(c) <= char('z'):
  64.             val = (toAscii(c) - toAscii('a')) + 10
  65.         if val >= radix:
  66.             raise Exception("        invalid character!")
  67.             return ret
  68.         ret += val
  69.     return ret
  70. asc as List = [
  71.     "NUL", "SOH", "STX", "ETX", "EOT",
  72.     "ENQ", "ACK", "BEL", "BS", "HT",
  73.     "LF", "VT", "FF", "CR", "SO",
  74.     "SI", "DLE", "DC1", "DC2", "DC3",
  75.     "DC4", "NAK", "SYN", "ETB", "CAN",
  76.     "EM", "SUB", "ESC", "FS", "GS",
  77.     "RS", "US", "Spc"
  78. ]
  79. control as List = [
  80.     "NUL (null)",
  81.     "SOH (start of heading)",
  82.     "STX (start of text)",
  83.     "ETX (end of text)",
  84.     "EOT (end of transmission)",
  85.     "ENQ (enquiry)",
  86.     "ACK (acknowledge)",
  87.     "BEL (bell)",
  88.     "BS  (backspace)",
  89.     "TAB (horizontal tab)",
  90.     "LF  (line feed, NL new line)",
  91.     "VT  (vertical tab)",
  92.     "FF  (form feed, NP new page)",
  93.     "CR  (carriage return)",
  94.     "SO  (shift out)",
  95.     "SI  (shift in)",
  96.     "DLE (data link escape)",
  97.     "DC1 (device control 1)",
  98.     "DC2 (device control 2)",
  99.     "DC3 (device control 3)",
  100.     "DC4 (device control 4)",
  101.     "NAK (negative acknowledge)",
  102.     "SYN (synchronous idle)",
  103.     "ETB (end of trans. block)",
  104.     "CAN (cancel)",
  105.     "EM  (end of medium)",
  106.     "SUB (substitute, EOF end of file)",
  107.     "ESC (escape)",
  108.     "FS  (file separator)",
  109.     "GS  (group separator)",
  110.     "RS  (record separator)",
  111.     "US  (unit separator)",
  112. ]
  113. def makeTable():
  114.     sbuf = ""
  115.     abuf = ""
  116.     tbuf = ""
  117.     sbuf = "    "
  118.     for i in range(0, 8):
  119.         sbuf += "+----"
  120.     sbuf += "+"
  121.     println(sbuf)
  122.     sbuf = "    "
  123.     sbuf += "| 0- "
  124.     sbuf += "| 1- "
  125.     sbuf += "| 2- "
  126.     sbuf += "| 3- "
  127.     sbuf += "| 4- "
  128.     sbuf += "| 5- "
  129.     sbuf += "| 6- "
  130.     sbuf += "| 7- "
  131.     sbuf += "|"
  132.     println(sbuf)
  133.     sbuf = "+---"
  134.     for i as int in range(0, 8):
  135.         sbuf += "+----"
  136.     sbuf += "+" 
  137.     println(sbuf)
  138.     for i as int in range(0, 16):
  139.         tbuf = ""
  140.         sbuf = convertItoA(i, 16)
  141.         tbuf += "| " + sbuf + " "
  142.         for j as int in range(0, 8):
  143.             k as int = j*16 + i
  144.             if j*16 + i <= 32:
  145.                 abuf = string.Format("| {0,-3}", asc[j*16 + i])
  146.             elif j*16 + i == 127:
  147.                 abuf = string.Format("| {0,-3}", "DEL")
  148.             else:
  149.                 c = cast(char, j*16 + i)    // casting from int to char
  150.                 abuf = string.Format("| {0,2} ", c)
  151.             tbuf += abuf
  152.         tbuf += "|"
  153.         println(tbuf)
  154.     sbuf = "+---"
  155.     for i in range(0, 8):
  156.         sbuf += "+----"
  157.     sbuf += "+"
  158.     println(sbuf)
  159.     println("")
  160.     for i in range(0, 16):
  161.         tbuf = string.Format("{0,-30}  {1,-34}", control[i], control[i+16])
  162.         println(tbuf)
  163. if len(argv) > 1 and "-h" == argv[0]:
  164.     printUsage()
  165.     Environment.Exit(1)
  166. makeTable()



실행> booi makeAsciiTable.boo

    +----+----+----+----+----+----+----+----+
    | 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
Posted by Scripter
,